简体   繁体   English

用户登录时的关系条件

[英]Relationship condition if user is logged in

I want to create a relationship that checks if a user has liked a post. 我想创建一个检查用户是否喜欢帖子的关系。 In order to do this, the relationship needs to check if the user is logged in, and then use their user_id to get the like record. 为此,该关系需要检查用户是否已登录,然后使用其user_id获取类似记录。 Something like: 就像是:

public function userLike() {
    if (Auth::check()) return $this->hasOne('App\Like')->where('user_id', Auth::user()->id);
}

However, this doesn't work. 但是,这不起作用。 Additionally, if the user is not logged in and this relationship is called (which it is by default), it will return an error. 此外,如果用户未登录并且调用了此关系(默认情况下为该关系),它将返回错误。

What is the proper way of doing this? 正确的做法是什么?

If you want to conditionally load a relation then you can do so by lazy eager loading. 如果要有条件地加载关系,则可以通过延迟加载来完成。 In your example you could define the relation as 在您的示例中,您可以将关系定义为

public function userLike() {
    return $this->hasOne('App\Like', 'user_id');
}  

Then in your controller (or wherever else) you can do the check for if the user is question is currently logged in user or not 然后,在您的控制器(或其他任何地方)中,您可以检查用户是否有问题,当前是否已登录该用户。

$loggedInUser = auth()->user(); 
if($loggedInUser){
    $loggedInUser->load('userLike');
}  

Then you can continue with whatever you want to do with the loggedInUser and the userLike . 然后,你可以继续你想用做任何loggedInUseruserLike

Say you have multiple users at a point (in your code) and you want to load the likes for only the currently logged in user then you can 假设您在某个位置(在您的代码中)有多个用户,并且只想为当前登录的用户加载点赞,则可以

//$users is a collection of multiple users - assumed
foreach($users as $user){
    if($user->email === auth()->user()->email){
        $user->load('userLike');
    }
}  

Hope this helps 希望这可以帮助

This is how I would do that: 这就是我要做的:

1) “likes” table 1)“喜欢”表

I would first create a table to store all the “likes” with columns for a “ post_id ” and a “ user_id 我首先要创建一个表来存储所有“喜欢”,并在其中存储“ post_id ”和“ user_id ”的列

2) create the relationships 2)建立关系

User Model 用户模型

public function likes()
{
    return $this->belongsToMany('App\Post', 'likes', 'user_id', 'post_id');
}

Post Model 邮政模型

public function likes()
{
    return $this->belongsToMany('App\User', 'likes');
}

3) Post Controller - the method 3)Post Controller-方法

I would do the followings: 我将执行以下操作:

  • check if user is logged in, otherwise throw an error 检查用户是否登录,否则抛出错误
  • make sure post exists, otherwise throw an error 确保帖子存在,否则引发错误
  • create a new entry in like table with user_id and post_id 在类似表中使用user_idpost_id创建一个新条目

(you can also check if the user is logged in directly on your route using a middleware) (您还可以使用中间件检查用户是否直接在您的路线上登录)

4) In the view 4)在视图中

I would call the controller method using an ajax call. 我将使用ajax调用来调用控制器方法。

Not sure if I forgot something, but hopefully that can help you a little bit. 不知道我是否忘记了什么,但希望可以对您有所帮助。

If you need to “like” more things than only post, check the polymorphic relationships. 如果您不仅需要“喜欢”更多信息,请检查多态关系。

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

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