lundi 8 octobre 2018

eager load model ids only

Using Laravel 5.1, I have deeply connected and nested models for my HTML5 game. When the player logs in, it loads their profiles.

Each profile has m:m completed quests, m:m completed tasks, m:m completed minigames, etc.

The quests/tasks/minigames are belongsTo relationship, i.e., Task belongsTo Quest, Minigame belongsTo Task, etc.

Eager loading these on the user->profile then takes a ton of time.

What I need to do instead then is eager load only the IDs of tasks, minigames, etc for the profile. I tried this via $appends:

class Profile extends BaseModel
{
    protected $with = ['game', 'quests'];

    protected $appends = ['task_ids'];

    public function getTaskIdsAttribute()
    {
        return $this->tasks->pluck('task_id');
    }

Still, this loads the models and an array of two null task Id values (The loaded task models eager load with their related children too.):

  • task_ids is an array with two null values.

  • tasks is an array with two eager loads Task models.

enter image description here

I need to speed up login so how can I load IDs only without the rest of the attributes?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire