laravel多条件连接

nkkqxpd9  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(304)

我有一行代码:

$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

我做错什么了?

zu0ti5jz

zu0ti5jz1#

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

$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();

相关问题