Laravel雄辩-关系字段不相等 [英] Laravel Eloquent - where relationship field does not equal

查看:70
本文介绍了Laravel雄辩-关系字段不相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为这很简单,但是目前还没玩.

I thought this would be fairly simple but it's not playing ball currently.

对于这个问题,我有2个表格,应用程序"和""application_call_logs".

I have 2 tables for this question, 'applications' & 'application_call_logs'.

此查询需要从application表中返回所有数据,其中最新的呼叫日志的状态都不为X.

This query needs to return all from the applications table where the latest call log doesn't have a status of X.

这是当前查询:

$query = Application::query();

$query->where(function($query) {
    $query->whereDoesntHave('call_logs');
    $query->orWhereHas('latest_call_log', function($q) {
        $q->where('status', '!=', 'not interested');
    });
});

return $query->get();

这应该返回所有没有呼叫日志,或者最新的呼叫日志没有等于特定字符串的状态字段的行.

This should return all rows that either have no call logs, or where the latest call log doesn't have the status field equaling a specific string.

这里:

$q->where('status', '!=', 'not interested');

即使call_logs具有多于1行,即使我正在查询最新的关系,也似乎没有任何影响.我还验证了最新消息是否返回了正确的最新记录.

Seems to have no affect if the call_logs has more than 1 row, even though I'm querying the latest relationship. I've also verified the latest is returning the correct latest record.

应用程序模型中的两个关系是:

The two relationships in the Application model are:

public function call_logs()
{
    return $this->hasMany('App\ApplicationCallLog', 'lead_id', 'id');
}

public function latest_call_log()
{
    return $this->hasOne('App\ApplicationCallLog', 'lead_id', 'id')->latest();
}

检查生成的SQL:

select * from `applications` where (not exists (select * from `lead_call_logs` where `applications`.`id` = `lead_call_logs`.`lead_id`) or exists (select * from `lead_call_logs` where `applications`.`id` = `lead_call_logs`.`lead_id` and `status` != ?))

推荐答案

有一种解决方案应该适合这种情况:

there is a solution around should be good for this situation:

我认为这行代码的星期:

i think that this line has the week point of the code:

return $this->hasOne('App\ApplicationCallLog', 'lead_id', 'id')->latest();

这应该是hasMany,但是您可以使用hasOne将结果限制为一个.

this should be hasMany, but you use hasOne to limit the result to one.

,如果您尝试过:

 return $this->hasMany('App\ApplicationCallLog', 'lead_id', 'id')->latest()->limit(1);

它根本行不通,因为对于所有结果,结果都将限制为ApplicationCallLog ....

it simply won't work, because the result will be limited to ApplicationCallLog for all of the results ....

将会,有一个软件包staudenmeir/eloquent-eager-limit专门针对这种情况而制作:

will, there is a package staudenmeir/eloquent-eager-limit that is made especially for this situations:

composer require staudenmeir/eloquent-eager-limit:"^1.0"

class Application extends Model
{
use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
public function latest_call_log()
{
    return $this->hasMany('App\ApplicationCallLog', 'lead_id', 'id')->latest()
->limit(1);
}

}

class ApplicationCallLog extends Model
{
use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
}

使用此程序包将限制查询中每个结果的ApplicationCallLog而不是所有结果的一个,这对于hasOne ...具有相同的作用.

using this package will limit ApplicationCallLog for every result in your query not one for all of the result, and that will have the same effect for hasOne ....

通过这个小改进,我认为:

with this minor enhancement, i think:

$q->where('status', '!=', 'not interested');

将起作用...

有关eloquent-eager-limit软件包的更多信息:

more about eloquent-eager-limit package in:

https://github.com/staudenmeir/eloquent-eager-limit

这篇关于Laravel雄辩-关系字段不相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆