[英]How to apply where clause on field of related table on Laravel eloquent
I have following query that I need to achieve in 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')
I have all relation set up in laravel. 我在laravel中建立了所有关系。
I am trying to achieve this like below: 我正在尝试实现以下目标:
$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();
This Eloquent query is giving expected result but taking too much time. 这个雄辩的查询给出了预期的结果,但是花费了太多时间。
Is there any other way to do that efficiently? 还有其他有效方法吗?
Your help is highly regarded. 您的帮助受到高度重视。
I hope the following code will be helpfull for you 希望以下代码对您有所帮助
$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.