我有以下需要在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();
关于php - 如何在Laravel Eloquent 表上对关联表的字段应用where子句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49312612/