lundi 21 février 2022

Relationship not loading in production

We have a laravel 5.5 app in production and recently we noticed that eager loading relationships seem to be failing.

The query that is failing is:

$allClients = Client::with([
        'tasks',
        'tasks.task_files',
        'resume_files',
    ])
    ->leftjoin('users', 'users.id', 'clients.client_id')
    ->where([
        ['status', 0],
        ['assigned_to', $user->id],
    ])
    ->orderBy('name', 'ASC')
    ->get();

The tasks.task_files relationship is not loading in production.

If I run the same on my local, it loads data fine.
If I run the same in a tinker shell on production, it loads data fine.
But when the controller is hit via a request, it doesn't load the task_files data.

I've tried logging the queries being run through QueryLog and they seem correct and running the same SQL queries gives the correct data.

The relationships are:

    Model: Client

    /**
     * This is used to get the tasks for the current client.
     *
     * @return Task $tasks[]
     */
    public function tasks()
    {
        return $this->hasMany('App\Task', 'client_id', 'client_id');
    }
    Model: Task

    /**
     * This is used to get the task files for the current task.
     *
     * @return TaskFile $files[]
     */
    public function task_files()
    {
        return $this->hasMany('App\TaskFile', 'task_id', 'task_id')->whereIn('type', ['type1', 'type2'])->latest('updated_at');
    }

I have no clue why would this suddenly stop working in production.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire