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

Dee*_*ens 14 php queue laravel laravel-5 laravel-horizon

我正在使用Laravel构建API,并希望使用Laravel Notifications系统发送推送通知.我有一个匹配模型(基本上是一个帖子),另一个用户可以喜欢这个匹配.当喜欢匹配时,帖子的创建者将获得推送通知.它就像Instagram,Facebook等.

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

Laravel Horizo​​n列表

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

错误如下:

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();
}
Run Code Online (Sandbox Code Playgroud)

通知:

<?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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Bit*_*Bay -2

检查您的 .env 以确保您确实使用 REDIS

BROADCAST_DRIVER=redis
CACHE_DRIVER=redis
SESSION_DRIVER=redis
SESSION_LIFETIME=120
QUEUE_DRIVER=redis
Run Code Online (Sandbox Code Playgroud)

然后清除缓存(php artisan cache:clear , php artisan view:clear),这应该可以解决问题

编辑

我遇到了类似的问题,但现在我只使用 Docker,之前我必须检查缓存的配置文件、错误的文件/文件夹权限等(REDIS 仅用于广播,其他是标准的)。我开始只使用 redis - 这对我来说更容易、更快、更容易调试!与 Docker 一起确实有助于不使用混乱的 nginx/apache/php/redis/ ...