如何在laravelsql查询中使用where子句中的对象

z9ju0rcb  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(404)

假设我有一些 doctor_id ```
{doctor_id: 1}
{doctor_id: 2}

现在我想找到那些被选中的人的名字 `doctor_id` 从 `doctors` table。 `doctors` 表字段是 `id` , `name` , `address` ....
我的问题是

$doctors_data = Doctor::whereIn('id', $doctor_id)->get();

但我有错误 `log` 文件
local.error:sqlstate[hy000]:常规错误:2031(sql:select*from) `doctors` 哪里 `id` 在(?)
有什么问题吗。请帮忙
waxmsbnn

waxmsbnn1#

可以使用 pluck 方法,可以将其作为第二个参数传递给 whereIn 方法

$doctorids = $doctorscollection->pluck('doctor_id');
$doctors_data = Doctor::whereIn('id', $doctorids)->get();
iovurdzv

iovurdzv2#

whereIn 要求第二个参数是数组。
i、 对你来说, wherein 应该是这样的。 $doctorsData = Doctor::whereIn('id', [1,2])->get(); 不知道你手上有什么东西 $docter_id ,但问题的解决方案是,您需要将对象转换为一个id数组,然后再将其馈送到该数组中 whereIn .
希望你能理解。如果仍然无法解决问题,您可以与我们分享您的意见 doctor_id 对象,以便我们可以帮助将其转换为数组。

相关问题