laravel作业/通知失败 [英] laravel job/notification failing

查看:120
本文介绍了laravel作业/通知失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的网站上建立联系表单,以便当某人单击发送"时,将运行一个作业,并且在该作业中,将向所有管理员用户发送通知.我在失败的工作表中仍然收到此错误:

I am trying to set up a contact form on my site whereby when someone clicks send, then a job is run and in that job, a notification is sent to all admin users. I keep getting this error in my failed jobs table though:

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

我到处都是代码,看不到我做错了什么.有人可以帮忙吗?

I have been all over my code and I can't see what I have done wrong. Would anyone be able to help please?

这是我的控制人:

<?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');
    }
}

我的工作:

<?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));
    }
}

我的通知:

<?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 [
            //
        ];
    }
}

奇怪的是,当我在作业的handle方法中运行die dump时,它永远不会触发,但是工匠队列工作人员说它已被正确处理,但随后的通知却失败了.我不确定为什么在工作中不会使用该handle方法.

The weird thing is that when I run a die dump in the handle method of the job, it never fires, but the artisan queue worker says it was processed correctly but the subsequent notification is where it is failing. I am not sure why that handle method in the job wouldn't be firing.

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

I have set my .env file to database queue driver.

我认为可能是我没有导入联系人模型,但是您可以看到我的联系模型.

I thought it might be that I didn't import the contact model, but you can see I have.

任何帮助将不胜感激.

推荐答案

可能是因为作业和通知都已排队,所以可以说该联系人可能正在丢失".尝试使该作业不可排队,并且仅将通知排队(或相反).或完全取消作业,然后从控制器发送通知.

Could be that because both the job and the notification are queued, the contact could be getting 'lost in transit' so to speak. try making the job non queueable, and only queue the notification (or the other way around). Or scrap the job altogether and just send the notification from the controller.

这篇关于laravel作业/通知失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆