samedi 29 juillet 2017

Optimize Eloquent query

I have the following eloquent relation

User Model

public function supplies()
{
    return $this->hasMany('App\Supply', 'employee');
}

Supply Model

public function user()
    {
        return $this->belongsTo('App\User', 'employee');
    }


    public function projects()
    {
        return $this->belongsTo('App\Project', 'project');
    }

Project Model

public function supplies()
{
    return $this->hasMany('App\Supply', 'project');
}

I try to get all supplies per year on projects with the following query

$supplies = Project::with('supplies')->whereHas('supplies', function ($query){
           $query->with('user.skills')->where('deleted', 0)->whereIn('supply_status', [1,2])->whereYear('time_from', 2017);
        })->orderBy('title')->get();

supplies table has lot of entries and I have memory limit problems. How can I optimize to get the best result?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2uHO1xT
via IFTTT

Aucun commentaire:

Enregistrer un commentaire