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

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


This is not a complete solution, but it will lower your chances of running into this error in the future.

Instead of passing in the whole Match model into the job, only pass the id of the model. You can then fetch that model in the constructor.

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

Notification:

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

You can use the SerializesModels trait, but it doesn't work well when you add a delay to a queued job. This is because it will try to reload the model on __wakeup() and sometimes it cannot find the class.

Hopefully this helps :)


Its probably because $user is not an object of User model, its an object of Match model. You need to do a User::findorfail or User::firstOrFail then notify the user.

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

}

Unless the notify trait is used in Match model. Or you could use eager loading which will cost way less queries!


Check your .env to be sure that u really use REDIS

BROADCAST_DRIVER=redisCACHE_DRIVER=redisSESSION_DRIVER=redisSESSION_LIFETIME=120QUEUE_DRIVER=redis

then clear cache ( php artisan cache:clear , php artisan view:clear ), that should clear the issue

EDIT

I had similar problems but now I use Docker only and before I had to check for cached configfiles, wrong file/folderpermissions and so on (REDIS for broadcast only, others were standard). I started using redis only - that`s a lot easier, faster and more debugfriendly for me ! And together with Docker really helpful to not use messed up nginx/apache/php/redis/ ...