laravel eloquent 急切加载嵌套条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26586719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:20:21  来源:igfitidea点击:

laravel eloquent eager loading nested condition

phplaraveleager-loading

提问by rfpdl

I have 2 tables that is using eager loading and then using nested condition in that eager loading:

我有 2 个表使用预先加载,然后在该预先加载中使用嵌套条件:

//migration for lead table
public function up()
{
    Schema::create('leads', function(Blueprint $table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('first_name',255);
        $table->string('surname',255);
    });

    Schema::table('leads', function($table)
    {
        $table->foreign('create_by')->references('id')->on('employees')->onDelete('cascade');
    });
}

//lead for lead detail emails
public function up()
{
    Schema::create('lead_detail_emails', function(Blueprint $table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('lead_id')->unsigned();
        $table->string('email',255);
    });

    Schema::table('lead_detail_emails',function($table)
    {
        $table->foreign('lead_id')->references('id')->on('leads')->onDelete('cascade');
    });
}

//leads model
class LeadsModel extends Eloquent
{
    protected $table = 'leads';

    public function emails()
    {
        return $this->hasMany('LeadDetailEmailsModel','lead_id','id');
    }
}

//lead detail emails
class LeadDetailEmail extends Eloquent
{
    protected $table = 'lead_detail_email';

    public function lead()
    {
        return $this->belongsTo('LeadsModel');
    }
}

When I am trying to add nested condition to eager loading, lets say

当我尝试向急切加载添加嵌套条件时,可以说

$qry = LeadsModel::with(
                            array
                            (
                            'emails' => function($qr)
                            {
                                $qr->orWhere('email','like','%testname%');
                            }
                       ));

$res = $qry->get();

dd($res);

It returns all the records in the lead, I have tried joining emails and $qry by using

它返回领先的所有记录,我尝试使用加入电子邮件和 $qry

->join('lead_detail_emails','lead_detail_emails.lead_id','=','leads.id');

but it does not work as well. may I know what is the problem in the code?

但它不起作用。我可以知道代码中有什么问题吗?

update question

更新问题

how can i get the leads by doing nested condition on the emails?

如何通过对电子邮件执行嵌套条件来获取潜在客户?

回答by Jarek Tkaczyk

$qry = LeadsModel::with(array('emails' => function ($q) use ($input) {
            $q->where('email','like',"%{$input}%");
        }))->whereHas('emails', function ($q) use ($input) {
            $q->where('email','like',"%{$input}%");
        });

$res = $qry->get();