计算 post 有多少评论 - Laravel 5.2
Count how many reviews a post has - Laravel 5.2
我需要计算 post 有多少评论。我该怎么做?
这是我的 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'));
}
这是我的评论table:
以下应该有效
$listing = Listing::with('reviews')->orderByRaw(function($listing)
{
return $listing->review->count();
}, desc)->take(10)->get();
试试这个
$review = Review::orderBy('-- most reviews here --');
$reviewCount = $review->count();
$listings=$review->take(10)->get();
在您的列表模型中:
public function countReview() {
return count($this->reviews);
}
在您看来:
{{$listing->countReview()}}
也许在控制器中你可以这样写:
public function mostReviews() {
Review::orderBy('listing', 'desc')->blahblah; // or 'asc'
}
我还没有测试过,但下面的查询(使用查询生成器)应该可以满足您的需求 - 评论最多的 10 个列表。此外 average_stars
列应 return 平均星级率。您可以通过将 orderBy
更改为 average_stars
.
轻松修改查询以获取 10 个评分最高的列表
$listings = \DB::table('reviews AS r')
->select([
'r.listing_id',
\DB::raw('COUNT(*) AS no_of_reviews'),
\DB::raw('AVG(r.stars) AS average_stars'),
'l.*'])
->join('listings AS l', 'l.id', '=', 'r.listing_id')
->limit(10)
->orderBy('no_of_reviews', 'DESC')
->groupBy('listing_id')
->get();
请注意版本最高为 Laravel 5.2 版,这将 return 个 stdObject
数组。您可以使用与 Eloquent Collection.
类似的方式轻松访问 blade 模板中的那些内容
@foreach($listings as $listing)
<tr>
<td>{{ $listing->id }}</td>
<td>{{ $listing->title }}</td>
<td>{{ $listing->no_of_reviews }}</td>
<td>{{ floor($listing->average_stars) }}</td>
</tr>
@endforeach
这是我做的:
public function mostReviews() {
$reviews = DB::table('reviews')
->select(DB::raw('AVG(stars) as review_score, listing_id'))
->groupBy('listing_id')
->orderBy('review_score', 'desc')
->limit(10)
->get();
foreach($reviews as $key => $review) {
$reviews[$key] = Listing::find($review->listing_id);
}
return view('listings.most-reviews', [
'listings' => $reviews
]);
}
我需要计算 post 有多少评论。我该怎么做?
这是我的 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'));
}
这是我的评论table:
以下应该有效
$listing = Listing::with('reviews')->orderByRaw(function($listing)
{
return $listing->review->count();
}, desc)->take(10)->get();
试试这个
$review = Review::orderBy('-- most reviews here --');
$reviewCount = $review->count();
$listings=$review->take(10)->get();
在您的列表模型中:
public function countReview() {
return count($this->reviews);
}
在您看来:
{{$listing->countReview()}}
也许在控制器中你可以这样写:
public function mostReviews() {
Review::orderBy('listing', 'desc')->blahblah; // or 'asc'
}
我还没有测试过,但下面的查询(使用查询生成器)应该可以满足您的需求 - 评论最多的 10 个列表。此外 average_stars
列应 return 平均星级率。您可以通过将 orderBy
更改为 average_stars
.
$listings = \DB::table('reviews AS r')
->select([
'r.listing_id',
\DB::raw('COUNT(*) AS no_of_reviews'),
\DB::raw('AVG(r.stars) AS average_stars'),
'l.*'])
->join('listings AS l', 'l.id', '=', 'r.listing_id')
->limit(10)
->orderBy('no_of_reviews', 'DESC')
->groupBy('listing_id')
->get();
请注意版本最高为 Laravel 5.2 版,这将 return 个 stdObject
数组。您可以使用与 Eloquent Collection.
@foreach($listings as $listing)
<tr>
<td>{{ $listing->id }}</td>
<td>{{ $listing->title }}</td>
<td>{{ $listing->no_of_reviews }}</td>
<td>{{ floor($listing->average_stars) }}</td>
</tr>
@endforeach
这是我做的:
public function mostReviews() {
$reviews = DB::table('reviews')
->select(DB::raw('AVG(stars) as review_score, listing_id'))
->groupBy('listing_id')
->orderBy('review_score', 'desc')
->limit(10)
->get();
foreach($reviews as $key => $review) {
$reviews[$key] = Listing::find($review->listing_id);
}
return view('listings.most-reviews', [
'listings' => $reviews
]);
}