我有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');
}
}
当我尝试将嵌套条件添加到预先加载时,让我们说
$qry = LeadsModel::with(
array
(
'emails' => function($qr)
{
$qr->orWhere('email','like','%testname%');
}
));
$res = $qry->get();
dd($res);
它返回了潜在客户中的所有记录,我尝试使用
加入电子邮件和$ qry->join('lead_detail_emails','lead_detail_emails.lead_id','=','leads.id');
但它不起作用。我可以知道代码中的问题是什么吗?
更新问题
如何通过在电子邮件上执行嵌套条件来获取潜在客户?
答案 0 :(得分:11)
$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();