簡體   English   中英

查詢Laravel在所有id相等的情況下向很多人口述

[英]Query Laravel Eloquent many to many where all id's are equal

我創建了一個基於Laravel的項目,並且當attribute_company用作連接companiesattributes表的數據透視表時, companiesattributesattribute_company與Many To Many關系相關。

我從客戶端獲得了一系列attribute_id ,我需要獲得具有完整屬性的公司的結果。

我找到的唯一解決方案是查詢whereHas whereIn inside里面,如下所示:

Company::whereHas('attributes', function (Builder $query) use ($atts_ids) {
     $query->whereIn('attribute_id', $atts_ids);
})->get();

如果找到至少一個attribute_id (這不是我要查找的內容),此查詢將返回companies

如果有人能讓我更清楚的話會很棒。

謝謝大家 :)

一種可能的方案:

$company = new Company();
$company = $company->newQuery();

foreach($atts_ids as $att_id)
{
    $company = $company->whereHas('attributes', function (Builder $query) use ($att_id) {
        $query->where('attribute_id', $att_id);
    });
}

$company = $company->get();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM