【发布时间】:2020-03-14 09:36:15
【问题描述】:
我希望用户只对产品进行一次评论(评分),我看到 this 并尝试过,但我收到错误 Object of class Illuminate\Database\Eloquent\Collection could not be converted to int 我该如何解决这个问题,以便用户能够评论产品一次。
产品.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');
}
刀片文件
@foreach($products as $product)
@if ($product->currentUserHasSubmittedReview() == false )
<a " href="#openModal-about">Write review</a>
@else
@endif
@endforeach
【问题讨论】:
-
将
$countOfReviews > 1更改为$countOfReviews->count()
标签: php laravel laravel-5 eloquent