spatie / laravel-interacts-with-payload
向Laravel应用中所有作业的负载中添加变量
1.2.0
2024-03-08 13:11 UTC
Requires
- php: ^8.2
- illuminate/contracts: ^10.0|^11.0
- nesbot/carbon: ^2.72|^3.0
- spatie/laravel-package-tools: ^1.4.3
Requires (Dev)
- nunomaduro/collision: ^7.0|^8.0
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.5
- spatie/laravel-ray: ^1.28
README
此包使得向每个作业中注入内容变得容易。
想象一下,你希望在所有队列作业中都有启动作业的用户信息。使用此包就是这样实现的。
// typically in a service provider use Spatie\InteractsWithPayload\Facades\AllJobs; AllJobs::add('user', fn() => auth()->user());
要获取你的队列作业中的用户,你可以调用getFromPayload
,这是通过InteractsWithPayload
特质可用的。
use Illuminate\Contracts\Queue\ShouldQueue; use Spatie\InteractsWithPayload\Concerns\InteractsWithPayload; class YourJob implements ShouldQueue { use InteractsWithPayload; public function handle() { // instance of User model or `null` $user = $this->getFromPayload('user'); } }
你是视觉学习者吗?
在这个直播中,你将看到我们的开发者Freek解释如何使用此包,它的内部结构和如何测试此包。
支持我们
我们投入了大量资源来创建一流的开放源代码包。你可以通过购买我们的付费产品之一来支持我们。
我们非常感谢你从家乡寄给我们明信片,注明你正在使用我们的哪个包。你可以在我们的联系页面上找到我们的地址。我们在我们的虚拟明信片墙上发布所有收到的明信片。
安装
您可以通过composer安装此包
composer require spatie/laravel-interacts-with-payload
用法
要向所有作业添加值,请使用带有名称和返回值的闭包,在AllJobs
外观上调用add
方法。
use Spatie\InteractsWithPayload\Facades\AllJobs; AllJobs::add('extraValue', fn() => 'My extra value')
要获取你的队列作业中的用户,你可以调用getFromPayload
,这是通过InteractsWithPayload
特质可用的。
use Illuminate\Contracts\Queue\ShouldQueue; use Spatie\InteractsWithPayload\Concerns\InteractsWithPayload; class YourJob implements ShouldQueue { use InteractsWithPayload; public function handle() { // will contain "My extra value" $value = $this->getFromPayload('extraValue'); } }
使用模型
将闭包传递给add
返回Eloquent模型是安全的。
use Spatie\InteractsWithPayload\Facades\AllJobs; AllJobs::add('user', fn() => auth()->user())
你可以使用getFromPayload
检索模型
use Illuminate\Contracts\Queue\ShouldQueue; use Spatie\InteractsWithPayload\Concerns\InteractsWithPayload; class YourJob implements ShouldQueue { use InteractsWithPayload; public function handle() { // instance of User model or `null` if the user has been deleted in the meantime $user = $this->getFromPayload('user'); } }
测试
composer test
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献。
安全漏洞
有关如何报告安全漏洞,请参阅我们的安全策略。
致谢
此包受到这篇精彩的博客文章的启发,作者是James Brooks。感谢James帮助我们追踪Laravel中的排队错误👍
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。