首页 文章

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

提问于
浏览
13

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

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

Laravel Horizon list

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

错误如下:

Illuminate \ Database \ Eloquent \ ModelNotFoundException:/home/forge/owowgolf.com/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php中的模型[App \ Models \ Match] 118没有查询结果: 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;
    }
}

1 回答

  • -2

    检查你的.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 / ...

相关问题