如何使用Laravel中的管道
本篇内容主要讲解“如何使用Laravel中的管道”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用Laravel中的管道”吧!

创新互联总部坐落于成都市区,致力网站建设服务有成都做网站、成都网站建设、网络营销策划、网页设计、网站维护、公众号搭建、小程序开发、软件开发等为企业提供一整套的信息化建设解决方案。创造真正意义上的网站建设,为互联网品牌在互动行销领域创造价值而不懈努力!
一、控制器
路由器部分
Route::get('/pipe', ['as'=>'pipe', 'uses'=>'PipeController@index']);控制代码
input('name');
// $name = Str::random(10);
return app(Pipeline::class)
->send($name)
->through($this->pipes)
->then(function ($content) {
return User::create([
'name' => $content,
'email'=>Str::random(10).'@gmail.com',
'password'=>Hash::make('password'),
]);
});
}
}二、管道部分
目录结构如下:
├─app │ │ User.php │ ├─Http │ │ ... │ │ │ ├─Models │ │ ... │ │ │ ├─Pipes │ │ │ BothSidesWords.php │ │ │ LeftWords.php │ │ │ RightWords.php │ │ │ │ │ └─Contracts │ │ PipeContracts.php
interface的代码 路径app/Pipes/Contracts/Pipe.php下的代码如下:三个管道的类的代码
LeftWords.php的代码LeftWords.php的代码BothSidesWords.php的代码
这里我们使用管道默认的方法handle,你可以自定义方法名。像下面这样定义myHandleMethod为处理方法名称。
return app(Pipeline::class)
->send($name)
->through($this->pipes)
->via('myHandleMethod')
->then(function ($content) {
return User::create([
'name' => $content,
'email'=>Str::random(10).'@gmail.com',
'password'=>Hash::make('password'),
]);
});你这样定义后,修改你的interface,同时修改你的实现类即可。
三、结果说明
访问http://localhost/pipe?name=lisa之后,能成功打印出获取的结果。User表内部,有数据保存成功。
{
"name": "[left-lisa-right]",
"email": "3riSrDuBFv@gmail.com",
"updated_at": "2020-09-05T05:57:14.000000Z",
"created_at": "2020-09-05T05:57:14.000000Z",
"id": 15
}到此,相信大家对“如何使用Laravel中的管道”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
网页名称:如何使用Laravel中的管道
链接分享:http://www.jxjierui.cn/article/ghdspg.html


咨询
建站咨询
