【问题标题】:How to make a laravel like counter?如何制作类似laravel的计数器?
【发布时间】:2017-08-08 17:57:30
【问题描述】:

我希望每个帖子都显示其总点赞数。

我的 Blade.php 文件中有一个这样的循环:

        @foreach ($posts as $post)
          <article class="post" data-postid="{{ $post->id }}">
                <p>{{ $post->body }}</p>
                <div class="info">
                    Posted by {{ $post->user->first_name }} on {{ $post->created_at }}
                </div>
                <div class="interaction">
                  {{ $countlike->where(['post_id' => $post->id])->get()->count() }}<a href="#" class="like"> {{ Auth::user()->likes()->where('post_id', $post->id)->first() ? Auth::user()->likes()->where('post_id', $post->id)->first()->like == 1 ? 'You Liked This Post' : 'Like' : 'Like' }}</a> |
                  {{ $countdislike->where(['post_id' => $post->id])->get()->count() }}<a href="#" class="like"> {{ Auth::user()->likes()->where('post_id', $post->id)->first() ? Auth::user()->likes()->where('post_id', $post->id)->first()->like == 0 ? 'You Disliked This Post' : 'Dislike' : 'Dislike' }}</a> |
                  @if(Auth::user() == $post->user)
                  <a href="#" class="edit">Edit Post</a> |
                  <a href="{{ route('post.delete', ['post_id' => $post->id]) }}">Delete</a>
                  @endif
                </div>
            </article>
            <br>

        @endforeach

这是我的控制器功能:

  public function getDashboard(Request $request) {
     $posts = Post::orderBy('created_at', 'desc')->get();
     $countlike = Like::where(['like' => '1']);
     $countdislike = Like::where(['like' => '0']);
     return view('dashboard')->with(['posts' => $posts])->with(['countlike' =>   $countlike])->with(['countdislike' => $countdislike]);
  }

数据库中的点赞数:

数据库中的帖子:

结果:

虽然循环中的第一个帖子与 ORM 通信,但其余的则不。 我无法将 eloquent ORM 集成到 Blade.php 文件中的循环中,我在这里做错了什么?

【问题讨论】:

  • 你能用不同的话解释一下,你到底想完成什么?

标签: php database laravel frameworks eloquent


【解决方案1】:

问题

在循环中,这里的where 函数{{ $countlike-&gt;where(['post_id' =&gt; $post-&gt;id])-&gt;get()-&gt;count() }} 将继续向同一个对象$countlike 添加更多where 条件,$countdislike 也是如此,在第一个循环之后最终将不返回任何结果。

解决方案

获得每个帖子的点赞数的更好方法是通过 Post 和 Like 之间的一对多关系,然后您可以在循环中访问它

$post-&gt;likes()-&gt;where(['like' =&gt; '1'])-&gt;count()点赞

$post-&gt;likes()-&gt;where(['like' =&gt; '0'])-&gt;count() 表示不喜欢

更新

正如Nickstery 所指出的,避免在循环中进行数据库查询。使用with 将喜欢的内容与帖子一起加载

//Get posts and likes with most recent posts on top
$posts = Post::with('likes')->latest()->get(); 
//then in the loop
$post->likes->where('like', 1)->count() //for likes
$post->likes->where('like', 0)->count() //for dislikes

【讨论】:

    【解决方案2】:

    兄弟,很简单

    public function getDashboard(Request $request) {
         $count = '1';
         $posts = Post::groupBy('post_id')
                      ->where('like',$count)->get();
         $like_count = count($post);
         return view('dashboard', compact('like_count') );
    }
    

    现在将 flog 值设为 1=>like 或 0=>unlike,首先将您的帖子分组并选择仅喜欢的帖子。接下来 make count 函数用于计算 post 之类的数字并放入新变量中。用于将该变量传递给刀片文件的紧凑方法

    【讨论】:

      【解决方案3】:

      切勿在循环内使用数据库查询。通过一个 DB 查询在 PHP 中获取所需的数据并将其发送到查看。 在帖子和点赞中使用模型relations 你会像这样重写你的代码

      $postsWithLikes = Post::with('likes')->all();
      View::share('posts', $postsWithLikes);
      return view('my_view');
      

      【讨论】:

      • 谢谢你的评论,我会调整的。
      猜你喜欢
      • 2019-11-08
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 2020-10-07
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多