【问题标题】:Count how many reviews a post has - Laravel 5.2计算一篇文章有​​多少评论 - Laravel 5.2
【发布时间】: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


    【解决方案1】:

    以下应该可以工作

    $listing = Listing::with('reviews')->orderByRaw(function($listing)
    {
        return $listing->review->count();
    }, desc)->take(10)->get();
    

    【讨论】:

    • 这可能有效,但我需要按大多数评论对每个帖子进行排序,从高到低。目前没有显示任何内容。
    • 尝试在take(10)之后添加-&gt;get()
    • 它给了我这个错误:strtolower() 期望参数 1 是字符串,给定对象
    • 看看我在上面的编辑,这让我可以算出所有 3 星的 rieviews,但我只需要找出最高的评论
    【解决方案2】:

    试试这个

    $review = Review::orderBy('-- most reviews here --');
    $reviewCount = $review->count();
    $listings=$review->take(10)->get();
    

    【讨论】:

      【解决方案3】:

      在您的列表模型中:

        public function countReview() { 
        return count($this->reviews); 
        } 
      

      在你看来:

       {{$listing->countReview()}} 
      

      也许在控制器中你可以写这样的东西:

       public function mostReviews() { 
        Review::orderBy('listing', 'desc')->blahblah; // or 'asc'
       }
      

      【讨论】:

      • $this->products 来自哪里?
      • 将该代码放入您的列表模型中,我创建了自定义函数来计算评论。对不起,我忘记了,我正在从我的项目中复制代码
      • 我正在使用我当前项目中的代码,它可以工作
      【解决方案4】:

      我尚未对其进行测试,但下面的查询(使用查询生成器)应该可以为您提供您想要的 - 10 个评论最多的列表。此外,average_stars 列应返回平均星级。您可以通过将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 以上的版本,这将返回 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
      

      【讨论】:

        【解决方案5】:

        这就是我所做的:

        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
          ]);
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-20
          • 1970-01-01
          • 2016-03-01
          • 2017-05-10
          相关资源
          最近更新 更多