doushi6932 2014-09-04 09:30
浏览 18
已采纳

Laravel 4显示用户登录后属于用户的帖子

Im learning Laravel right know and im bumped in to a little problem.

I have created a login system that works just fine. When a user is logged in he can create different posts.

Now the problem is when i login with for example Bob and create posts i can se all Bobs post in his profile page and that is OK!

But the problem i have is when i login with Alice i can see Bobs post and thats what i cant figure out how to solve.

I have some code that set the relations between user and post:

     //Post Model
    public function users(){
      return $this->hasMany('Posts');
}

    //User Model
   public functon posts(){

      return $this->belongsTo('User');
}

Now how can i show Alice posts only when i login to Alice account?

  • 写回答

1条回答 默认 最新

  • dongliao1860 2014-09-04 09:46
    关注

    You can create a simple check on the profile page. Like comparing the id of the user who is logged in with the one whose profile is being accessed. If the Id matches that means the logged in user is the same as the profile user can see all of his posts while if the logged in user id is not equals to the id of user whose profile is being accessed posts will not be visible.

    To get the posts of a a user you can do

    $posts = Post::where('user_id','=', Auth::id())->get();
    

    Don't know how your code is assembled so can't exactly write a code, but the above logic will work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改