2017-06-16 134 views
1

我有3個模型:標記,發佈,用戶,並將它們映射到TagPost,TagUser模型。在關係laravel中優化查詢

當用戶在輸入搜索中搜索標籤時,我使用$ http.post(VUE)並將選擇的數組標籤發送到服務器。

代碼示例,在服務器打印:

array:2 [ 
     0 => array:10 [ 
     "id" => 197 
     "name" => "nisi" 
     "slugify" => "nisi" 
     "thumbnail" => "http://lorempixel.com/400/400/?85397" 
     "seo_title" => null 
     "seo_description" => null 
     "seo_keyword" => null 
     "cover" => null 
     "created_at" => "2017-06-13 07:19:30" 
     "updated_at" => "2017-06-04 06:01:14" 
     ] 
     1 => array:10 [ 
     "id" => 184 
     "name" => "dolores" 
     "slugify" => "dolores" 
     "thumbnail" => "http://lorempixel.com/400/400/?45793" 
     "seo_title" => null 
     "seo_description" => null 
     "seo_keyword" => null 
     "cover" => null 
     "created_at" => "2017-06-08 15:37:59" 
     "updated_at" => "2017-05-27 13:34:02" 
     ] 
    ] 

我想,當用戶搜索的標籤,然後查看更新用戶和帖子有標籤的用戶搜索。

查看這裏:

enter image description here

在服務器(laravel)如何與關係模型中使用至少查詢代碼?

+0

強權這可以幫助您:https://開頭laravel。 COM /文檔/ 5.4 /口才,關係 –

回答

0

假設你已經正確設置你的關係,這應該工作

Tag::with('posts','users')->whereIn('id', $your_ids_send_by_vue)->get(); 

然後在你的控制器做邏輯得到你想要的數據