我想获得我所有在拉拉韦尔(Laravel)的朋友的身份 [英] I want to get status of all my friend in laravel

查看:33
本文介绍了我想获得我所有在拉拉韦尔(Laravel)的朋友的身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取我的朋友的身份,即我(正在等待或他接受了我的请求,或者他阻止了我)为了这我得到了我所有的朋友,除了登录一个,并获得了他们各自的状态

I want to get the status of my friend i-e (pending or he accepted my request or he blocks me) for this I get all my friend except login one and get their respective status

$user=User::with(["friendships"])->where('id','!=',Auth::user()->id)->get();

我的模特

function friendships() {
    return $this->hasMany('App\Models\Friendship','first_user','id');
}

我的友谊数据库表就是这样

my friendship database table is like that

id  first_user  second_user   status

其中第一个用户是发送请求的用户,第二个用户是接收请求的用户,其状态为未决,已接受或已阻止.那么我该如何加载所有朋友并检查状态.

where the first user is the one who sends the request and the second_user is the one who receives the request, where the status is either pending, accepted, or blocked .so how could I load all my friends and check the status.

推荐答案

好吧,首先您说我",想得到我所有的友谊.因此查询将需要为此进行更改.您现在拥有的以下查询如下:

Ok so firstly you say "I" want to get all my friendships. So the query would need to change for this. The following query that you have right now is as follows:

$user=User::with(["friendships"])->where('id','!=',Auth::user()->id)->get();

这将获得不是当前登录用户的所有用户.您想要获得当前经过身份验证的用户并加载友谊关系来执行此操作,您可以按照以下步骤进行操作:

This is getting ALL users that are NOT the currently logged in user. What you want is to get your currently authenticated user and load in the friendships relationship to do this you can do as follows:

$user = User::with('friendships')->find(Auth::id());

因此,要通过-> find(Auth :: id())进行分解,您可以获取具有该ID的用户.其次,您只返回一个用户,而不是使用-> get()会获得的用户集合.

So to break this down with ->find(Auth::id()) you are getting the user with that id. Second you are returning only one user not a collection of users which you would get by using ->get().

然后您可以在刀片文件中执行类似的操作.

Then you could do something like this in a blade file.

@foreach($user->friendships as $friendship)
    $friendship->status
@endforeach

所以我对情况的误解是,要获得所有用户,然后附加与当前用户的友情状态,如果他们看起来像一个人

So my misunderstanding of the situation, for getting all users and then attaching a status of a friendship with the current user if they had one could look like

$authenticatedUserId = Auth::id();

$users = User::with([
    'friendships' => function ($query) {
        $query->where('first_user', $authenticatedUserId);
    }
]);

然后显示如下:

@foreach($users as $user)
    @if($user->friendships->count() > 0)
        $user->friendships->first()->status // A user should only have one friendship with another user
    @else
        // No friendship so add a button to add a friendship
    @endif
@endforeach

请注意,这仅包括发送朋友请求,不包括其他用户发送给您的任何请求.但是它们将出现在用户列表中,因此您可能会遇到重复的好友请求,在这种情况下,您需要将其过滤掉.请查看文档,以获取有关此操作的更多帮助 https://laravel.com/docs/8.x/eloquent-relationships

Note this would only include send friend requests, not any sent to you by another user. But they would be in the list of users so you could end up with duplicate friend requests you would need to filter these out in this case. Checkout the documentation for more help on doing this https://laravel.com/docs/8.x/eloquent-relationships

这篇关于我想获得我所有在拉拉韦尔(Laravel)的朋友的身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆