首页 文章

具有关系的laravel中的优化查询

提问于
浏览
1

我有3个型号: Tag, Post, User ,并用 TagPost, TagUser 型号映射它们 .

当用户在输入搜索中搜索标签时,我使用$ http.post(VUE)并将选择的数组标签发送到服务器 .

Example tags print in server:

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"
      ]
    ]

我想当用户搜索标签然后查看更新用户和帖子有标签用户搜索 .

View here:

enter image description here

In server (laravel) how to code with relationship model using least query?

1 回答

  • 0

    假设你已经正确设置了你的关系,这应该有效

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

    然后在您的控制器中执行逻辑以获取所需数据

相关问题