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

时间:2022-12-21 01:32:26

I want every post to show its total number of likes.

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

I have a loop like this in my blade.php file:

我的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

This is my controller function:

这是我的控制器功能:

  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: 如何制作像柜台一样的laravel?

喜欢数据库:

Posts in the database: 如何制作像柜台一样的laravel?

数据库中的帖子:

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

While the first post in the loop communicates with ORM, the rest doesn't. I cannot integrate eloquent ORM to the loop in the blade.php file, what am I doing wrong here?

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

3 个解决方案

#1


1  

Problem

In the loop, the where function here {{ $countlike->where(['post_id' => $post->id])->get()->count() }} will keep adding more where conditions to the same object $countlike, same for $countdislike which will eventually return no result after the first loop.

在循环中,where函数在这里{{$ countlike-> where(['post_id'=> $ post-> id]) - > get() - > count()}}将继续添加更多条件到相同的条件对象$ countlike,对于$ countdislike也是如此,它最终会在第一个循环后没有返回结果。

Solution

A better way to get the likes count for each post will be through a one-to-many relationship between Post and Like which you can then access in the loop as such

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

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

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

$post->likes()->where(['like' => '0'])->count() for dislikes

$ post-> likes() - > where(['like'=>'0']) - > count()for dislikes

Update

As pointed out by Nickstery, avoid making database queries inside a loop. Use with to eager load the likes along with the posts

正如Nickstery所指出的那样,避免在循环内部进行数据库查询。与...一起使用以热切加载喜欢的内容

//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


1  

Never use DB queries inside loops. Grab the needed data in PHP by one DB query and send it to view. Use model relations inside Post and Likes and you will rewrite your code like

切勿在循环内使用数据库查询。通过一个数据库查询在PHP中获取所需的数据并将其发送到视图。在Post和Likes中使用模型关系,你将重写你的代码

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

#3


0  

bro, its very simple

兄弟,很简单

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

now make flog value 1=>like or 0=>unlike, first group your post and select only liked post . next make count function used to count number like post and into new variable . compact method used to pass that variable to blade file

现在使flog值1 =>喜欢或0 =>不像,首先对你的帖子进行分组,然后只选择喜欢的帖子。下一个make count函数用于计数数字,如post和新变量。用于将该变量传递给刀片文件的紧凑方法

#1


1  

Problem

In the loop, the where function here {{ $countlike->where(['post_id' => $post->id])->get()->count() }} will keep adding more where conditions to the same object $countlike, same for $countdislike which will eventually return no result after the first loop.

在循环中,where函数在这里{{$ countlike-> where(['post_id'=> $ post-> id]) - > get() - > count()}}将继续添加更多条件到相同的条件对象$ countlike,对于$ countdislike也是如此,它最终会在第一个循环后没有返回结果。

Solution

A better way to get the likes count for each post will be through a one-to-many relationship between Post and Like which you can then access in the loop as such

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

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

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

$post->likes()->where(['like' => '0'])->count() for dislikes

$ post-> likes() - > where(['like'=>'0']) - > count()for dislikes

Update

As pointed out by Nickstery, avoid making database queries inside a loop. Use with to eager load the likes along with the posts

正如Nickstery所指出的那样,避免在循环内部进行数据库查询。与...一起使用以热切加载喜欢的内容

//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


1  

Never use DB queries inside loops. Grab the needed data in PHP by one DB query and send it to view. Use model relations inside Post and Likes and you will rewrite your code like

切勿在循环内使用数据库查询。通过一个数据库查询在PHP中获取所需的数据并将其发送到视图。在Post和Likes中使用模型关系,你将重写你的代码

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

#3


0  

bro, its very simple

兄弟,很简单

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

now make flog value 1=>like or 0=>unlike, first group your post and select only liked post . next make count function used to count number like post and into new variable . compact method used to pass that variable to blade file

现在使flog值1 =>喜欢或0 =>不像,首先对你的帖子进行分组,然后只选择喜欢的帖子。下一个make count函数用于计数数字,如post和新变量。用于将该变量传递给刀片文件的紧凑方法