我目前正在开发Laravel博客App . 我想显示帖子是由登录用户加入书签还是不在刀片模板视图上 .
点击帖子的书签按钮,我调用ajax功能,这是正常的 .
但当我请求页面 /posts
时,它会显示所有带有相同颜色书签的帖子
但没有显示带红色的书签帖子书签按钮
我想区分帖子在页面渲染上,如果它是由登录用户进行了预订 .
我在mysql数据库中有3个表: users
, posts
, bookmarks
表
结构用户表:(id,name,email,password,...)
结构帖子表:(id,title,body,user_id,...)
结构书签表:(user_id,post_id,created_at,updated_at)
这是用于在刀片视图中显示书签图标的代码
<span class="desktop-post-read-later">
@if(Auth::guest())
<span class="post-read-later" data-type="add" data-id="{{$post->id}}" title="Please login to bookmark this post!">
<i class="bookmark fa fa-bookmark-o" id="bookmark_{{$post->id}}"></i>
</span>
@else
<span class="post-read-later" data-type="add" data-id="{{$post->id}}">
@if( ????? what condition here i have to put ??????)
<i class="bookmark fa fa-bookmark-o" id="bookmark_{{$post->id}}" style="color:red"></i>
@else
<i class="bookmark fa fa-bookmark-o" id="bookmark_{{$post->id}}"></i>
@endif
</span>
@endif
2 回答
Auth::user()
返回当前登录的用户 .您的帖子表已经
user_id
. 您只需检查它是否与已登录用户的user_id相同:@if($post->user_id == Auth::user()->id)
---------编辑----------
因此,您必须加入数据透视表才能知道已记录的用户是否已为帖子添加了书签:
原始查询:
使用查询构建器:
然后,使用
@if
条件中的已添加书签的列 .我有答案,在我的
PostsController
上添加以下方法并在刀片视图上