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

Php 没有模型[App\Models\Match]的查询结果,php,laravel,laravel-5,queue,laravel-horizon,Php,Laravel,Laravel 5,Queue,Laravel Horizon,我正在使用Laravel构建一个API,并希望使用Laravel通知系统发送推送通知。我有一个匹配模型(基本上是一个帖子),其他用户可以喜欢这个匹配。如果喜欢匹配,帖子的创建者将收到推送通知。就像Instagram、Facebook等 推送通知通常不会发送给用户。我安装了Laravel Horizon以查看是否存在错误。有时发送通知,有时不发送。使用完全相同的数据: 有时,使用完全相同的数据(相同的用户、相同的匹配项)时,通知会失败 错误如下: Illumb\Database\Eloquent

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

推送通知通常不会发送给用户。我安装了Laravel Horizon以查看是否存在错误。有时发送通知,有时不发送。使用完全相同的数据:

有时,使用完全相同的数据(相同的用户、相同的匹配项)时,通知会失败

错误如下:

Illumb\Database\Eloquent\ModelNotFoundException:无查询结果 对于中的[App\Models\Match]118型号 /home/forge/owowgolf.com/vendor/laravel/framework/src/illusted/Database/elounce/Builder.php:312

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

控制器方法:

/**
 * 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;
    }
}
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...
}

检查你的.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/…真的很有帮助

这可能是因为$user不是用户模型的对象,而是匹配模型的对象。您需要执行User::findorfail或User::first或fail,然后通知用户

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


除非在匹配模型中使用notify特征。或者你可以使用即时加载,这将减少查询的成本

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

不要将整个
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特性,但在向排队作业添加延迟时,该特性无法正常工作。这是因为它将尝试在
\uuu wakeup()
上重新加载模型,有时它找不到该类


希望这有帮助:)

显示通知文件的代码。添加了
NewLikeOnPost
通知类的代码。我不确定,但是,在您试图访问作业中的会话数据的任何地方,或者在队列中处理的任何函数或对象都在使用会话?您可能面临postgresql行锁,无论原因是什么(主要是过度使用currentUser()方法)。查看这篇文章可能会对您有所帮助:它与使用相同队列系统的相同服务器上的多个环境有关。暂存的事件/作业正在生产队列中执行。未修复:(这不是答案