首页 > 解决方案 > 将当前用户发送到 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 时也会出现此错误。

标签: php

解决方案


推荐阅读