samedi 1 mai 2021

Laravel Eloquent perform Aggregation function in the pivot relationship

I would like to perform the average rating of respective product which stored in the pivot table called "reviews".Below are my existing code.

Product Model:

public function reviews(){
        return $this->belongsToMany(User::class,'reviews')->withPivot('comment','rating')->withTimestamps();
    }
public function getProductRatingAttribute(){
        return $this->reviews()->average('rating');
    }

User Model:

public function reviews(){
        return $this->belongsToMany(Product::class,'reviews')->withPivot('comment','rating')->withTimestamps();
    }

Migration for the reviews

Schema::create('reviews', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('product_id');
            $table->string('comment')->nullable();
            $table->integer('rating');
            $table->foreign('user_id')->references('id')->on('users');
            $table->foreign('product_id')->references('id')->on('products');
        });

I have attempted to use the below to code construct the expected output but it leads N+1 issue because it does not take advantage of the eager loading of the Laravel Eloquent.

$s = Product::all()->append('product_rating');

As I check telescope, it produces 6 query which can lead performance issue when the data is large. enter image description here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire