首页 文章

如何在Laravel博客应用中的Blade模板视图中由Loggedin用户为书签或不加书签?

提问于
浏览
0

我目前正在开发Laravel博客App . 我想显示帖子是由登录用户加入书签还是不在刀片模板视图上 .

点击帖子的书签按钮,我调用ajax功能,这是正常的 .

但当我请求页面 /posts 时,它会显示所有带有相同颜色书签的帖子
enter image description here

但没有显示带红色的书签帖子书签按钮

enter image description here
我想区分帖子在页面渲染上,如果它是由登录用户进行了预订 .

我在mysql数据库中有3个表: userspostsbookmarks

结构用户表:(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 回答

  • 0

    Auth::user() 返回当前登录的用户 .

    您的帖子表已经 user_id . 您只需检查它是否与已登录用户的user_id相同:

    @if($post->user_id == Auth::user()->id)

    ---------编辑----------

    因此,您必须加入数据透视表才能知道已记录的用户是否已为帖子添加了书签:

    原始查询:

    select post.*, bookmarks.id as bookmarked from posts
    left join bookmarks on bookmarks.post_id = posts.id and bookmarks.user_id = Auth::user()->id
    

    使用查询构建器:

    DB::table('post')->leftJoin('bookmarks', function($join) {
        $join->on('post.id', '=', 'bookmarks.post_id');
        $join->on('bookmarks.user_id', '=', DB::raw(Auth::user()->id);
    })->select('post.*', 'bookmarks.id as bookmarked')
    ->get();
    

    然后,使用 @if 条件中的已添加书签的列 .

  • 1

    我有答案,在我的 PostsController 上添加以下方法

    function get_user_bookmark_postids(){
        $postsids=[];
        if(Auth::check()){
            $data = DB::table('posts')
            ->join('bookmarks', 'posts.id', '=', 'bookmarks.post_id')
            ->where('bookmarks.user_id', '=',Auth::user()->id)
            ->select('posts.id')
            ->get();
            $plucked = $data->pluck('id');
            $postsids=$plucked->all();
            return $postsids;
        }else{
            $postsids=[];
        }
    }
    
     public function index()
    {   
        $bookmarked_posts=$this->get_user_bookmark_postids();
        return view('user.posts.index',compact('bookmarked_posts'));   
    }
    

    并在刀片视图上

    <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="fa fa-bookmark-o"></i>
               </span>
    
            @else
    
             <span class="post-read-later" data-type="add" data-id="{{$post->id}}">                                                 
             @if(in_array($post->id,$bookmarked_posts))
             <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   
    </span>
    

相关问题