gpt4 book ai didi

php - 没有模型 [App\Models\Match] 的查询结果

转载 作者:IT王子 更新时间:2023-10-29 00:06:22 27 4
gpt4 key购买 nike

我正在使用 Laravel 构建 API,并希望使用 Laravel 通知系统发送推送通知。我有一个匹配模型(基本上是一个帖子),另一个用户可以喜欢这个匹配。当比赛被点赞时,帖子的创建者将收到推送通知。就像 Instagram、Facebook 等。

推送通知通常不会发送给用户。我安装了 Laravel Horizo​​n 看看有没有错误。有时会发送通知,有时不会。使用完全相同的数据:

Laravel Horizon list

通知有时会因完全相同的数据(相同的用户,相同的匹配)而失败。

错误如下:

Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Models\Match] 118 in /home/forge/owowgolf.com/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:312

我确定匹配项和用户存在于数据库中,我在发送通知之前已经验证了这一点。有人知道出了什么问题吗?我在网上能找到的一切都是人们在将通知发送到队列之前没有保存他们的模型。但是如果模型不存在,代码将通知发送到队列中的那一行甚至都不会到达。由于路由/ Controller 中的隐式绑定(bind)。

Controller 方法:

/**
* Like a match.
*
* @param \App\Models\Match $match
* @return \Illuminate\Http\JsonResponse
*/
public function show(Match $match)
{
$match->like();

$players = $match->players()->where('user_id', '!=', currentUser()->id)->get();

foreach ($players as $user) {
$user->notify(new NewLikeOnPost($match, currentUser()));
}

return ok();
}

通知:

<?php

namespace App\Notifications;

use App\Models\Match;
use App\Models\User;
use Illuminate\Bus\Queueable;
use NotificationChannels\Apn\ApnChannel;
use NotificationChannels\Apn\ApnMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;

class NewLikeOnPost extends Notification implements ShouldQueue
{
use Queueable;

/**
* The match instance.
*
* @var \App\Models\Match
*/
private $match;

/**
* The user instance.
*
* @var \App\Models\User
*/
private $user;

/**
* Create a new notification instance.
*
* @param \App\Models\Match $match
* @param \App\Models\User $user
*/
public function __construct(Match $match, User $user)
{
$this->user = $user;
$this->match = $match;

$this->onQueue('high');
}

/**
* Get the notification's delivery channels.
*
* @param \App\Models\User $notifiable
* @return array
*/
public function via($notifiable)
{
if ($notifiable->wantsPushNotification($this)) {
return ['database', ApnChannel::class];
}

return ['database'];
}

/**
* Get the mail representation of the notification.
*
* @param \App\Models\User $notifiable
* @return \NotificationChannels\Apn\ApnMessage
*/
public function toApn($notifiable)
{
return ApnMessage::create()
->badge($notifiable->unreadNotifications()->count())
->sound('success')
->body($this->user->username . ' flagged your match.');
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'user_id' => $this->user->id,
'body' => "<flag>Flagged</flag> your match.",
'link' => route('matches.show', $this->match),
'match_id' => $this->match->id,
];
}

/**
* Get the match attribute.
*
* @return \App\Models\Match
*/
public function getMatch()
{
return $this->match;
}
}

最佳答案

这不是一个完整的解决方案,但它会降低您将来遇到此错误的几率。

不是将整个 Match 模型传递到作业中,而是只传递模型的 id。然后,您可以在构造函数中获取该模型。

/**
* Like a match.
*
* @param \App\Models\Match $match
* @return \Illuminate\Http\JsonResponse
*/
public function show(Match $match)
{
$match->like();

$players = $match->players()->where('user_id', '!=', currentUser()->id)->get();

foreach ($players as $user) {
$user->notify(new NewLikeOnPost($match->id, currentUser()->id));
}

return ok();
}

通知:

class NewLikeOnPost extends Notification implements ShouldQueue
{
use Queueable;

private const QUEUE_NAME = 'high';

/**
* The match instance.
*
* @var \App\Models\Match
*/
private $match;

/**
* The user instance.
*
* @var \App\Models\User
*/
private $user;

/**
* Create a new notification instance.
*
* @param int $match
* @param int $user
*/
public function __construct(int $matchId, int $userId)
{
$this->user = User::query()->where('id', $userId)->firstOrFail();
$this->match = Match::query()->where('id', $matchId)->firstOrFail();

$this->onQueue(self::QUEUE_NAME);
}

// Rest of the class is still the same...
}

您可以使用 SerializesModels 特性,但是当您向排队的作业添加延迟时它效果不佳。这是因为它会尝试在 __wakeup() 上重新加载模型,有时它找不到类。

希望这对您有所帮助:)

关于php - 没有模型 [App\Models\Match] 的查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46581470/

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