samedi 14 mars 2020

Collection could not be converted to int Laravel

I want a user to review(rate) a product only once, I saw this and tried but I'm getting an error Object of class Illuminate\Database\Eloquent\Collection could not be converted to int how can I fix this so that a user will be able to review a product once.

Product.php

public function reviews()
{
    return $this->hasMany(ProductReview::class);
}

 public function currentUserHasSubmittedReview(){
    $countOfReviews = $this->reviews()
        ->where('user_id', Auth::user()->id)
        ->where('product_id', $this->id)
        ->get();

    return ($countOfReviews > 1 ? true : false);  //Error comes from this line
}

ProductReview.php

 public function product()
  {
    return $this->belongsTo('App\Product');
  }

Blade file

  @foreach($products as $product)
     @if ($product->currentUserHasSubmittedReview() == false )
     <a " href="#openModal-about">Write review</a>
      @else

      @endif
  @endforeach


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire