mercredi 5 juin 2019

Laravel Query n+1 - Querying custom attribute relationship count

On my User model I have the following...

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
 */
public function transactions(): HasManyThrough
{
    return $this->hasManyThrough(Transaction::class, Product::class);
}

/**
 * @return int
 */
public function getNumberOfOrdersInLastSevenDaysAttribute()
{
    $weekAgo = Carbon::now()->subWeek();
    return $this->transactions()->whereDate('transactions.created_at', '>', $weekAgo)->count();
}

When I get all Users using my custom attribute even eager loading transactions I get an n+1 issue.

enter image description here

What options do I have to omit my n+1 problem?



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2wD3csb
via IFTTT

Aucun commentaire:

Enregistrer un commentaire