Laravel 作业/通知失败

Leo*_*gal 3 laravel laravel-queue

我正在尝试在我的网站上设置一个联系表单,当有人单击“发送”时,就会运行一个作业,并在该作业中向所有管理员用户发送通知。不过,我在失败的作业表中不断收到此错误:

Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Contact]. in /var/www/html/leonsegal/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:412
Run Code Online (Sandbox Code Playgroud)

我已经检查了我的代码,但看不出我做错了什么。有人可以帮忙吗?

这是我的控制器:

<?php

namespace App\Http\Controllers;

use App\Contact;
use App\Jobs\SendContactJob;

class ContactController extends Controller
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('contact');
    }

    public function store()
    {
        request()->validate([
            'name' => 'required|max:255',
            'email' => 'required|email|unique:contacts|max:255',
            'message' => 'required|max:2000',
        ]);

        $contact = Contact::create(
            request()->only([
                'name',
                'email',
                'message',
            ])
        );

        SendContactJob::dispatch($contact);

        return back()->with('success', 'Thank you, I will be in touch as soon as I can');
    }
}
Run Code Online (Sandbox Code Playgroud)

我的工作:

<?php

namespace App\Jobs;

use App\Contact;
use App\Notifications\SendContactNotification;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Notification;

class SendContactJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $contact;

    /**
     * Create a new job instance.
     *
     * @param Contact $contact
     */
    public function __construct(Contact $contact)
    {
        $this->contact = $contact;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $users = User::all()
            ->where('admin', 1)
            ->where('approved', 1);

        Notification::send($users, new SendContactNotification($this->contact));
    }
}
Run Code Online (Sandbox Code Playgroud)

我的通知:

<?php

namespace App\Notifications;

use App\Contact;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class SendContactNotification extends Notification implements ShouldQueue
{
    use Queueable;

    protected $contact;

    /**
     * Create a new notification instance.
     *
     * @param $contact
     */
    public function __construct(Contact $contact)
    {
        $this->contact = $contact;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line($this->contact->name)
                    ->line($this->contact->email)
                    ->line($this->contact->message);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,当我在作业的句柄方法中运行模具转储时,它永远不会触发,但工匠队列工作人员说它已正确处理,但随后的通知是它失败的地方。我不确定为什么作业中的处理方法不会被触发。

我已将 .env 文件设置为数据库队列驱动程序。

我想可能是我没有导入联系人模型,但你可以看到我已经导入了。

任何帮助,将不胜感激。

Joh*_*sey 5

可能是因为作业和通知都已排队,所以可以说联系人可能会“在传输过程中丢失”。尝试使作业不可排队,并且仅对通知进行排队(或反之亦然)。或者完全放弃该作业并仅从控制器发送通知。

  • 这是因为作业和通知都已排队 - 我从通知中删除了应该队列并且它起作用了,谢谢! (2认同)