我正在學習 react native 并使用 laravel 作為后端。如何在laravel中撰寫一個動作來顯示用戶串列(供前端用戶搜索)以顯示每個人的友誼狀態對于授權用戶“Pending”,“Confirmed”,“Blocked”分別顯示所需的按鈕例如“添加朋友”、“接受朋友請求”、“從朋友中洗掉”和“阻止”?我的代碼:
友誼表遷移:
Schema::create('friendships', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('first_user')->index();
$table->unsignedBigInteger('second_user')->index();
$table->unsignedBigInteger('acted_user')->index();
$table->enum('status', ['pending', 'confirmed', 'blocked']);
$table->timestamps();
});
用戶模型:
//======================== functions to get friends attribute =========================
// friendship that this user started
protected function friendsOfThisUser()
{
return $this->belongsToMany(User::class, 'friendships', 'first_user', 'second_user')
->withPivot('status')
->wherePivot('status', 'confirmed');
}
// friendship that this user was asked for
protected function thisUserFriendOf()
{
return $this->belongsToMany(User::class, 'friendships', 'second_user', 'first_user')
->withPivot('status')
->wherePivot('status', 'confirmed');
}
// accessor allowing you call $user->friends
public function getFriendsAttribute()
{
if ( ! array_key_exists('friends', $this->relations)) $this->loadFriends();
return $this->getRelation('friends');
}
protected function loadFriends()
{
if ( ! array_key_exists('friends', $this->relations)) {
$friends = $this->mergeFriends();
$this->setRelation('friends', $friends);
}
}
protected function mergeFriends()
{
if($temp = $this->friendsOfThisUser) {
return $temp->merge($this->thisUserFriendOf);
} else {
return $this->thisUserFriendOf;
}
}
//======================== end functions to get friends attribute =========================
//====================== functions to get blocked_friends attribute ============================
// friendship that this user started but now blocked
protected function friendsOfThisUserBlocked()
{
return $this->belongsToMany(User::class, 'friendships', 'first_user', 'second_user')
->withPivot('status', 'acted_user')
->wherePivot('status', 'blocked')
->wherePivot('acted_user', 'first_user');
}
// friendship that this user was asked for but now blocked
protected function thisUserFriendOfBlocked()
{
return $this->belongsToMany(User::class, 'friendships', 'second_user', 'first_user')
->withPivot('status', 'acted_user')
->wherePivot('status', 'blocked')
->wherePivot('acted_user', 'second_user');
}
// accessor allowing you call $user->blocked_friends
public function getBlockedFriendsAttribute()
{
if ( ! array_key_exists('blocked_friends', $this->relations)) $this->loadBlockedFriends();
return $this->getRelation('blocked_friends');
}
protected function loadBlockedFriends()
{
if ( ! array_key_exists('blocked_friends', $this->relations)) {
$friends = $this->mergeBlockedFriends();
$this->setRelation('blocked_friends', $friends);
}
}
protected function mergeBlockedFriends()
{
if($temp = $this->friendsOfThisUserBlocked) {
return $temp->merge($this->thisUserFriendOfBlocked);
} else {
return $this->thisUserFriendOfBlocked;
}
}
// ======================================= end functions to get block_friends attribute =========
public function friend_requests()
{
return $this->hasMany(Friendship::class, 'second_user')
->where('status', 'pending');
}
控制器中的功能:
public function show(Request $request)
{
$user = auth()->user();
$users = User::where('id', '!=', $user->id)->get();
return UserInfoResource::collection($users);
}
我試圖在資源中做這樣的事情:
public function toArray($request)
{
return [
"id" => $this->id,
"uuid" => $this->uuid,
"first_name" => $this->first_name,
"last_name" => $this->last_name,
"avatar" => asset( 'storage/' . $this->avatar ),
"friendship" => $this->friends->only([auth()->user()->id])->first(),
// "friendship" => $this->pivot,
];
}
但->friends只顯示“已確認”的友誼,我如何跟蹤其余狀態?
uj5u.com熱心網友回復:
由于您已設定:
->wherePivot('status', 'confirmed');
您只會獲得具有該特定狀態的朋友。
有一個wherePivotIn()-method 允許您添加多個值來匹配。
將其更改為類似:
->wherePivotIn('status', ['confirmed', 'anotherStatus', ...etc...]);
也應該給你其他狀態的朋友。
您可以在手冊中閱讀有關該方法(和其他方法)的更多資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/401029.html
標籤:javascript php 拉拉维尔 反应原生 休息
上一篇:如何將公共螢屏添加到反應導航
