Laravel有多种条件的加入

时间:2018-11-05 15:21:14

标签: mysql laravel

我有这行代码:

$user['preference'] = PdTpreference::join('pd_tprofessional_preference', 'pd_tprofessional_preference.fkpreference', '=', 'pd_tpreference.pkpreference AND pd_tprofessional_preference.fkprofessional =' . $tmp->pkprofessional, 'left')->get();

但结果如下:

select * from `pd_tpreference` left join `pd_tprofessional_preference` on `pd_tprofessional_preference`.`fkpreference` = `pd_tpreference`.`pkpreference AND pd_tprofessional_preference`.`fkprofessional =12` where `pd_tpreference`.`deleted_at` is null

当我期望它像这样出现时:

select * from `pd_tpreference` left join `pd_tprofessional_preference` on `pd_tprofessional_preference`.`fkpreference` = `pd_tpreference`.`pkpreference` AND `pd_tprofessional_preference`.`fkprofessional` = 12 where `pd_tpreference`.`deleted_at` is null

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我通过以下方法解决了我的问题:

$user['preference'] = PdTpreference::leftJoin('pd_tprofessional_preference', function($q) use ($tmp){ $q->on('pd_tprofessional_preference.fkpreference', '=', 'pd_tpreference.pkpreference')->where('pd_tprofessional_preference.fkprofessional', '=', $tmp->pkprofessional); })->get();