如何制作像柜台一样的laravel?

时间:2017-03-17 08:56:10

标签: php database laravel frameworks eloquent

我希望每个帖子都能显示喜欢的总数。

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

喜欢数据库: likes in the database

数据库中的帖子: posts in the database

结果: enter image description here

虽然循环中的第一篇文章与ORM进行通信,但其余文章并没有。 我无法将有说服力的ORM集成到blade.php文件中的循环中,我在这里做错了什么?

3 个答案:

答案 0 :(得分:1)

从不在循环中使用数据库查询。通过一个数据库查询在PHP中获取所需的数据并将其发送到视图。 在帖子和喜欢中使用模型relations 你将重写你的代码,如

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

答案 1 :(得分:1)

<强>问题

在循环中,此处的where函数{{ $countlike->where(['post_id' => $post->id])->get()->count() }}会不断向同一个对象where添加更多$countlike个条件,$countdislike也会相同第一次循环后没有返回任何结果。

<强>解决方案

通过PostLike之间的一对多关系可以更好地获取每个帖子的点数,然后您可以在循环中访问

$post->likes()->where(['like' => '1'])->count()喜欢

$post->likes()->where(['like' => '0'])->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 :(得分:0)

兄弟,非常简单

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 =&gt;喜欢或0 =&gt;不像,首先对你的帖子进行分组,然后选择只有喜欢的帖子。下一个make count函数用于计数数字,如post和新变量。用于将该变量传递给刀片文件的紧凑方法