我正在使用hootlex的友谊包,但我不太确定如何当我收到所有好友请求并在foreach循环中显示它们时,获取用户信息(用户名等).
I'm using the friendship package by hootlex but I'm not quite sure how I get the user information (username etc.) when I'm getting all friend requests and displaying them in a foreach loop.
基本上我要做的是返回视图:
Basically what I have done is this, to return the view:
/** * @return mixed * * View friend requests */ public function getFriendRequests() { $user = Auth::user(); return view('friends.requests') -> with([ 'profile' => $user -> profile, 'user' => $user, 'received_requests' => $user -> getFriendRequests(), 'sent_requests' => $user -> getPendingFriendships() ]); }然后在我看来:
<div class="block block-green"> <div class="block-head"> <h4><i class="fa fa-fw fa-exclamation-circle"></i>Venneforespørsler ({{ count($received_requests) }})</h4> </div> <div class="block-body"> @foreach ($received_requests as $rr) <div class="request"> <div class="thumb">{{-- PROFILE THUMB HERE --}}</div> <div class="username">{{-- SENDERS USERNAME HERE --}}</div> <div class="actions"> <form method="post" action="/friends/request/accept/{{ $rr -> id }}"> <button type="submit" class="btn btn-success"><i class="fa fa-fw fa-check"></i>Aksepter</button> </form> <form method="post" action="/friends/request/deny/{{ $rr -> id }}"> <button type="submit" class="btn btn-danger"><i class="fa fa-fw fa-times"></i>Avslå</button> </form> </div> </div> @endforeach </div> </div>所以基本上我想知道的是,如何在每个好友请求中从User模型中获取用户名/其他信息?
So basically what I'm wondering, how would I get the username / other information from the User model on each friend request?
谢谢.
推荐答案您的 $ rr 变量包含类 Hootlex \ Friendships \ Models \ Friendship 的对象-您可以在此处查看其源代码: github /hootlex/laravel-friendships/blob/master/src/Models/Friendship.php .
Your $rr variable contains an object of class Hootlex\Friendships\Models\Friendship - you can see its source code here: github/hootlex/laravel-friendships/blob/master/src/Models/Friendship.php.
如您所见,该模型允许您通过同名属性访问给定朋友请求的发件人和收件人.
As you can see, this model allows you to access both sender and recipient of given friend request via properties of the same name.
因此,为了获取发件人的用户名,您需要使用以下命令进行访问:
Therefore, in order to get username of the sender, you'll need to access it with:
$rr->sender->username更多推荐
Laravel友谊获取用户信息
发布评论