【发布时间】:2016-11-10 00:54:51
【问题描述】:
我需要计算一个帖子有多少评论。我该怎么做呢?
这是我的 Listing.php 模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Listing extends Model
{
public function reviews()
{
return $this->hasMany('\App\Review');
}
}
这是我的 Review.php 模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Review extends Model
{
protected $fillable = ['stars','name','comment'];
public function listing()
{
return $this->belongsTo('\App\Listing');
}
}
这是我试图在控制器中计数的方法
public function mostReviews(Request $request) {
$listings = Review::orderBy('-- most reviews here --')->take(10)->get();
$headline = 'Most Reviewed Listings';
return view('pages.listings', compact('listings', 'headline'));
}
这是我的评论表:
【问题讨论】:
标签: php laravel relationship