gpt4 book ai didi

laravel 工作/通知失败

转载 作者:行者123 更新时间:2023-12-01 12:10:12 27 4
gpt4 key购买 nike

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

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

我遍历了我的代码,看不出我做错了什么。请问有人能帮忙吗?

这是我的 Controller :

<?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 时,它永远不会触发,但 artisan queue worker 说它已被正确处理,但随后的通知是它失败的地方。我不确定为什么作业中的 handle 方法不会触发。

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

我以为可能是我没有导入接触模型,但你可以看到我导入了。

如有任何帮助,我们将不胜感激。

最佳答案

可能是因为工作和通知都在排队,可以这么说,联系人可能会“在传输中丢失”。尝试使作业不可排队,并且仅将通知排队(或相反)。或者完全放弃作业,只从 Controller 发送通知。

关于laravel 工作/通知失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52266315/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com