[英]No query results for model [App\Models\Match]
我正在使用Laravel构建API,并希望使用Laravel Notifications系统发送推送通知。 我有一个比赛模型(基本上是帖子),另一个用户可以喜欢这个比赛。 当匹配被喜欢时,帖子的创建者将获得推送通知。 就像Instagram,Facebook等。
通常,推送通知没有发送给用户。 我安装了Laravel Horizon,以查看是否存在错误。 有时通知是发送的,有时不是。 使用完全相同的数据:
通知有时会失败,并使用完全相同的数据(相同的用户,相同的匹配项)。
错误如下:
Illuminate \\ Database \\ Eloquent \\ ModelNotFoundException:在/home/forge/owowgolf.com/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:312中没有对模型[App \\ Models \\ Match] 118的查询结果
我确定数据库中存在匹配项和用户,在发送通知之前,我已对此进行了验证。 有人知道出什么事了吗? 我在网上可以找到的所有内容是,在将通知发送到队列之前,人们没有保存他们的模型。 但是,如果模型不存在,则甚至不会到达代码将通知发送到队列的行。 由于路由/控制器中的隐式绑定。
控制器方式:
/**
* 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()
上重新加载模型,有时找不到该类。
希望这会有所帮助:)
这可能是因为$ user不是User模型的对象,而是Match模型的对象。 您需要执行User :: findorfail或User :: firstOrFail,然后通知用户。
public function show(Match $match)
{
$match->like();
$players = $match->players()->where('user_id', '!=', currentUser()->id)->get();
foreach ($players as $user) {
$someUser = User::findOrFail($user->user_id);
$someUser->notify(new NewLikeOnPost($match, currentUser()));
}
return ok();
}
除非在Match模型中使用notify特质。 或者,您可以使用急切的加载方式,减少查询费用!
检查您的.env,以确保您确实使用REDIS
BROADCAST_DRIVER=redis
CACHE_DRIVER=redis
SESSION_DRIVER=redis
SESSION_LIFETIME=120
QUEUE_DRIVER=redis
然后清除缓存( php artisan cache:clear , php artisan view:clear
),这应该清除问题
我遇到了类似的问题,但是现在我只使用Docker,然后才必须检查缓存的配置文件,错误的文件/文件夹权限等(REDIS仅用于广播,其他是标准的)。 我只开始使用redis-对我来说更容易,更快和更易于调试! 并与Docker一起使用对避免弄乱nginx / apache / php / redis /真的很有帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.