[英]how to join 3 table in laravel using database mongodb?
我們的應用程序具有3個模型“ Notification”,“ Postlike”和“ Post”
在通知模型中:
public function postlike()
{
return $this->belongsTo('App\Models\PostLikes', 'postlist');
}
public function post()
{
return $this->hasMany('App\Models\PostLikes', '_id', 'c_post_id');
}
在Postlike模型中:
public function postlist()
{
return $this->hasMany('App\Models\Post', '_id', 'c_post_id');
}
在通知存儲庫中:(查詢)
public function getnotification($userId)
{
$notification = $this->makeModel()
->with('post')
->with('postlike')
->with('notification')
->orderBy('created_at' , 'desc')
->where('replied_id', $userId)
->get();
return $notification;
}
有關更多信息,請附加以下圖像在此處輸入圖像描述
當我看到您的帖子時,我認為可以使用Laravel的內置eager loading
解決您的問題,因此它可以使您執行以下操作:
$notification = $this->makeModel()
->with('post', 'postlike', 'postlike.postlist')
//...
->get();
如果您的情況如此,這些鏈接應該可以解決問題:
https://laracasts.com/discuss/channels/eloquent/eager-load-multiple-nested-relationships
我不確定,但對於我來說似乎有點奇怪,“通知”模型與“后贊”模型有兩個關系。 也許您應該考慮使用一些數據透視表。
希望能幫助到你,
通知模型:
public function postlike()
{
return $this->belongsTo('App\Models\PostLikes', 'c_post_id');
}
Query:
$notification = $this->makeModel()
{
->with('post', 'postlike', 'postlike.postlist')->get()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.