簡體   English   中英

沒有有關模型[App \\ Models \\ Match]的查詢結果

[英]No query results for model [App\Models\Match]

我正在使用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();
}

通知:

<?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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM