简体   繁体   English

如何在Laravel雄辩的表上对关联表的字段应用where子句

[英]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.

相关问题 如何构建雄辩的查询以检索主表的记录,在Laravel 5 PHP中对其相关表应用where子句? - How to build Eloquent query to retrieve records of the main table, applying where clause on it's related table in Laravel 5 PHP? 在WHERE中应用OR子句,或在雄辩的laravel中应用HAVING - Apply OR clause in WHERE or HAVING in eloquent laravel JOIN表中的雄辩的where子句-Laravel 5.5 - Eloquent where clause in JOIN table - Laravel 5.5 使用 Eloquent 在 Laravel 中使用 where 子句访问嵌套的相关对象 - Access nested related objects with where clause in Laravel using Eloquent 如何简化 Laravel Eloquent where 子句? - How to simplify Laravel Eloquent where clause? 如何将数组传递给 Laravel eloquent 中的 where 子句? - how pass array to where clause in laravel eloquent? 如何在 Laravel Eloquent 中使用 GroupBy 和 Where 子句? - how to use GroupBy and Where Clause in Laravel Eloquent? 如何在laravel雄辩地按相关表的列排序? - how to order by column of related table in laravel eloquent? Laravel5 Eloquent从具有where子句的多个表中进行选择 - Laravel5 Eloquent select from multiple table with where clause Laravel 4雄辩的表联接和多个表上的where子句 - Laravel 4 Eloquent table join and where clause on multiple tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM