Laravel 计算列值为 1 的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37312942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:50:06  来源:igfitidea点击:

Laravel Count rows where column value is 1

phpmysqllaravel

提问by Michael Mano

Hey so i am trying to do an upvote downvote system and I am only using 1 table, the table columns are

嘿,所以我正在尝试做一个 upvote downvote 系统,我只使用 1 个表,表列是

      $table->increments('id');
      $table->integer('user_id')->unsigned();
      $table->foreign('user_id')->references('id')->on('users');
      $table->integer('voter_id')->unsigned();
      $table->foreign('voter_id')->references('id')->on('users');
      $table->boolean('vote_type')->default(0);
      $table->integer('commentable_id');
      $table->string('commentable_type');
      $table->string('unique_vote')->unique();

Basically I am trying to count how many votes the comment has but only where the vote_type is == 1and also the reverse for downvotes where the value is 0

基本上,我试图计算评论有多少票,但只计算价值所在的位置vote_type is == 1以及相反的反对票0

I was thinking about doing this with 2 different tables as it would make counting easier but I also dont want a large database.

我正在考虑用 2 个不同的表来做这件事,因为它会使计数更容易,但我也不想要一个大型数据库。

i know of {{$comment->votes->count()}}but this returns the total rows regardless of the vote_type value and I am wondering if anyone has a solution or knows of a way while keeping the queries low.

我知道,{{$comment->votes->count()}}但是无论 vote_type 值如何,这都会返回总行数,我想知道是否有人在保持低查询的同时有解决方案或知道方法。

回答by Achraf Khouadja

Why you do it like this

你为什么这样做

public function showCart() {

        $votes = Vote::where('vote_type',1)->count();
        // do stuff and then return the count with the actual data & view

    }

You cant chain like this

你不能像这样连锁

$votes = Vote::where('vote_type',1)->where('something',$something)->count();

if you want the result for the logged in user

如果您想要登录用户的结果

$votes = Auth::user()->votes->where('vote_type',1)->count();

I hope you get the point here, you dont have to do the count in blade

我希望你明白这一点,你不必计算刀片

回答by f_i

Too late to answer this question, but in general groupByon a collection can be a good option for this

回答这个问题为时已晚,但总的来说groupBy,收藏是一个不错的选择

$votesInGroups = Vote::all()->groupBy('vote_type');

if you want to refine the data:

如果要细化数据:

$votesInGroups->map(function($group, $key){
  // assign keys so that its meaningful
  if($key == 1) {
     return [
       'upvotes' => $group->count();
     ];
  }
  // yada yada yada
});

回答by Michael Mano

I ended up just creating another relation and then enquing it with the main call. eg comments class

我最终只是创建了另一个关系,然后用主调用将其加入。例如评论类

public function upvotes()
{
    return $this->morphMany('App\Models\General\Votes', 'commentable')->whereVoteType(1);
}
public function downvotes()
{
    return $this->morphMany('App\Models\General\Votes', 'commentable')->whereVoteType(0);
}

--

——

public function index($category_slug, $subcategory_slug, $post_slug)
{

  return view('pages.forum-post', [
    'post' => Post::With('comments','comments.user','comments.votes','comments.upvotes','comments.downvotes','comments.user.UserProfile')->whereSlug($post_slug)->firstOrFail()
  ]);
}

-- blade

- 刀刃

@if ($comment->upvotes)
  {{$comment->upvotes->count()}}
@endif
@if ($comment->downvotes)
  {{$comment->downvotes->count()}}
@endif
<hr />