php - 将当前用户发送到 laravel 中的工作
问题描述
我想在派遣工作时发送当前登录的用户数据。这个想法是使用 Rabbitmq 将当前用户从服务 1 发送到服务 2。然后在服务 2 中自动注册或登录该用户。我尝试过的内容:
服务一:
作业.php
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class Job implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public $data ;
public function __construct($data)
{
$this->data = $data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
}
}
火事件.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Jobs\Job;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
class FireEvent extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'fire';
public function handle()
{
Job::dispatch(Auth::user()->toArray());
}
}
服务 2:Job.php
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Auth;
class Job implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $data ;
public function __construct($data)
{
$this->data = $data;
}
public function handle()
{
echo 'Event: UserCreated' ;
echo json_encode($this->data) ;
}
}
事件服务提供者.php
<?php
namespace App\Providers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use App\Jobs\Job;
class EventServiceProvider extends ServiceProvider
{
public function boot()
{
\App::bindMethod(Job::class . '@handle' , function($job){
$job->handle();
});
}
}
Call to a member function toArray() on null
即使我已登录,当我运行 docker-compose run accounting-software php artisan fire 时也会出现此错误。
解决方案
推荐阅读
- lua - 使用 Lua 在 Splash 中查看返回的结果
- travis-ci - Travis CLI:使用“travis encrypt”加密返回 — 找不到资源 ({"file":"not found"})
- arduino - 为什么这会出现在我的串行监视器上?
- xamarin - 如何从 Xamarin.ios 知道哪个页面处于活动状态
- python - 如何在python中将字符串与变量进行数值积分?
- html - 如何使 html 表格列的宽度与其内容相同?
- javascript - 角循环json显示数据
- jquery - 甜蜜的警报计时器功能不适用于我的代码集
- python - 尝试所有解决方案后python中的导入错误
- javascript - 在对文件进行更改之前将导出 Blob