dst8922 2018-01-02 08:19
浏览 41
已采纳

如何在Laravel博客应用中的刀片模板视图中登录用户显示帖子是否有书签?

I'm currently working on Laravel blog App. I want to display posts is bookmarked by loggedin user or not on blade template view .

On Click of post's bookmark button , Im calling ajax function and that is working correctly.

but when i request to page /posts it shows all posts with same color bookmarks enter image description here

but not showing bookmarked posts bookmark button with red color

enter image description here i want to distinguish post on page render if it is bookmared by logged in user or not .

I have 3 tables in mysql database : users, posts, bookmarks table

structure users table : (id,name,email,password,...)

structure posts table : (id,title,body,user_id, ...)

structure bookmarks table : (user_id,post_id,created_at,updated_at)

here is code for showing bookmark icon in blade view

    <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条回答 默认 最新

  • dp610807 2018-01-03 14:35
    关注

    I have got my answer , by adding following method on my 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'));   
    }
    

    and on blade view

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?