简体   繁体   English

获取关系中的用户记录

[英]Get user record in relationship

I have 3 tables: 我有3张桌子:

  1. users table users
  2. posts table (users can have many posts) posts表(用户可以有多个帖子)
  3. likes table (works in the same way that Facebook likes do) likes表(工作方式与Facebook喜欢的方式相同)

I have the following relationships: 我有以下关系:

User.php : User.php

public function posts()
{
    return $this->hasMany('App\Post');
}

Post.php : Post.php

public function user()
{
    return $this->belongsTo('App\User');
}

public function likes()
{
    return $this->hasMany('App\Like');
}

Like.php : Like.php

public function post()
{
    return $this->belongsTo('App\Post');
}

To get all posts, I do: 要获取所有帖子,我要做:

$posts = Post::with('likes')
    ->get();

However, this returns all likes associated with the post (which I don't want). 但是,这将返回与该帖子相关的所有点赞(我不想要)。 What I need to be able to do is check if the logged in user has liked each post. 我需要做的是检查登录用户是否喜欢每个帖子。

For example, in my view, I have: 例如,在我看来,我有:

@foreach ($posts as $post)
    // check if logged in user liked the $post
@endforeach

How can I modify my code so that I can check if the user has liked each post and how can I do this by simply adding a new relationship ? 如何修改我的代码,以便可以检查用户是否喜欢每个帖子,以及如何通过简单添加新关系来做到这一点

First you need to create a relationship between users and likes 首先,您需要在用户和喜欢之间建立关系

User Model 用户模型

public function likes() {
    return $this->hasMany('App\Like');
}

Like Model 喜欢模特

public function user() {
    return $this->belongsTo('App\User');
}

Post Model 邮政模型

public function isLiked() {
    return (bool) $this->likes()->where('user_id', Auth::user()->id)->first(); 
    // Will return true if liked by the logged in user
}

Now you can use it like that: 现在,您可以像这样使用它:

if ( $post->isLiked() ) {
    // Do something
}

I'm pretty sure that you have make relation between Post with Likes, and Post with User models 我很确定您在具有喜欢的帖子和具有用户模型的帖子之间建立了联系

So... in Post model create public function, I think you have fields like post_id and user_id , to keep the likes. 所以...在Post模型中创建公共函数,我认为您拥有post_iduser_id之类的字段来保持喜欢。

Post Model: 帖子模型:

public function likes()
{
    return $this->belongsTo('App\Like', 'post_id);
}

after that in your controller make query to get the posts 之后,在您的控制器中进行查询以获取帖子

Post Controller 岗位控制器

 $post = Post::all();

and in the template, you can check if user_id is in this post like 在模板中,您可以检查user_id是否在此帖子中,例如

Post View 帖子查看

foreach($post as $p) {
  if($logged_user_id === $p->like()->user_id) {
    echo 'liked';
  } else {
   echo 'not liked';
  }
}

I think this will help you, (step-1) 我认为这会对您有所帮助(第1步)

    $post = Post::with(['Like.User' => function ($query) {
        $query->where('users.id', '=', Auth::user()->id);
    }])->get();       

Note:- users is table name. 注意:-用户是表名。 It will give all post and all like with user who is log in. 它会给所有帖子,并且与登录用户一样。

Another one solution, it will return all post with particular likes (likes by auth user) with user. 另一种解决方案,它将返回所有带有特定赞(由auth用户赞)的帖子。 (step-2) (第2步)

     foreach ($post as $item) {
        $like = $item->Like;
        foreach ($like as $key => $user){
            if(count($user->User) == 0){
                unset($like[$key]);
            }
        }
    }         

The result will be for step 1:- 结果将是步骤1:-

     [
       {
         "post" : "details",
         "like" : [ { 
                    "post_id" : 1,
                    "user_id" : 1 },
                    { 
                    "post_id" : 1,
                    "user_id" : null }],
       {
         "post" : "details",
         "like" : [] }
     ]

The result will be for step 2:- 结果将是步骤2:-

     [
       {
         "post" : "details",
         "like" : [ { 
                    "post_id" : 1,
                    "user_id" : 1 }],
       {
         "post" : "details",
         "like" : []  }
     ]        

The result keywords are assumption and the expectation is user_id in post and like tables, post_id in like table. 结果关键字是假设,期望值在post和like表中是user_id,在like表中是post_id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM