![](/img/trans.png)
[英]How to build Eloquent query to retrieve records of the main table, applying where clause on it's related table in Laravel 5 PHP?
[英]How to apply where clause on field of related table on Laravel eloquent
我有以下需要在Laravel雄辯中實現的查詢:
SELECT Q.quoteid
FROM `tblquote` Q
INNER JOIN tbladdress A ON A.addressid = Q.addressid
INNER JOIN tblquotecompany QC ON QC.quoteid = Q.quoteid
INNER JOIN tblcompany C ON C.companyid = QC.companyid
WHERE
Q.useremail = 'test@test' or
(Q.ipaddress = '000.00.00.' and A.zipcode = '00000')
我在laravel中建立了所有關系。
我正在嘗試實現以下目標:
$this->eloquentQuote->newQuery()
->with(EloquentQuote::RELATION_ADDRESS)
->with(EloquentQuote::RELATION_QUOTE_COMPANIES . '.' . EloquentQuoteCompany::RELATION_COMPANY)
->whereHas(EloquentQuote::RELATION_ADDRESS,
function ($query) use ($userEmail, $userIp, $zipCode) {
/** @var Builder $query */
$query->where([
[EloquentQuote::USER_EMAIL, '=', $userEmail],
])
->orWhere([
[EloquentQuote::IP_ADDRESS, '=', $userIp],
[EloquentAddress::ZIP_CODE, '=', $zipCode],
]);
})->get();
這個雄辯的查詢給出了預期的結果,但是花費了太多時間。
還有其他有效方法嗎?
您的幫助受到高度重視。
希望以下代碼對您有所幫助
$result = DB::table('tblquote')
->join('tbladdress', 'tbladdress.addressid', 'tblquote.addressid')
->join('tblquotecompany', 'tblquotecompany.quoteid', 'tblquote.quoteid')
->join('tblcompany', 'tblcompany.companyid', 'tblquotecompany.companyid')
->where('tblquote.useremail', 'test@test')
->orWhere([['tblquote.ipaddress','000.00.00.'], ['tbladdress.zipcode', '00000']])
->get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.