调用成员函数where()的数组

时间:2018-12-03 10:36:46

标签: php mysql laravel stored-procedures eloquent

我有以下MySQL例程存储过程“ Sel_All_Observation_SP”:-

    BEGIN
    SELECT Observations_TBL.observation_id, Observations_TBL.number, Observations_TBL.date, Observations_TBL.status, Observations_TBL.content, Observations_TBL.recommendation, Observations_TBL.remark, Users_TBL.User_name, Type_Name_TBL.Type_Name, Supervisors_TBL.supervisor_name
    FROM ((Observations_TBL INNER JOIN Supervisors_TBL ON (Supervisors_TBL.supervisor_id = Observations_TBL.supervisor_id) AND (Observations_TBL.supervisor_id = Supervisors_TBL.supervisor_id)) INNER JOIN Type_Name_TBL ON (Observations_TBL.Type_Name_ID = Type_Name_TBL.Type_Name_ID) AND (Observations_TBL.Type_Name_ID = Type_Name_TBL.Type_Name_ID)) INNER JOIN Users_TBL ON (Users_TBL.User_ID = Observations_TBL.user_id) AND (Observations_TBL.user_id = Users_TBL.User_ID);

END

我正在使用laravel查询生成器在laravel中调用上述过程:-

DB::select('call Sel_All_Observation_SP()')

上面以数组形式返回结果。

我正在尝试在上述查询中使用where语句:-

$observations = DB::select('call Sel_All_Observation_SP()')->where('user_id',1);

返回错误:-

  

Symfony \组件\调试\异常\   FatalThrowableError(E_ERROR)

     

调用数组中的成员函数where()

我甚至尝试通过以下方法将数组从数组转换为集合:-

$observations = collect(DB::select('call Sel_All_Observation_SP()')->where('user_id',1)->get());

它返回相同的错误

1 个答案:

答案 0 :(得分:1)

由于您没有选择表,因此where语句将不起作用。至于collect的代码,请尝试稍微更改一下语法,使其显示为:

$observations = collect(DB::select('call Sel_All_Observation_SP()'))->where('user_id',1);

以上代码将存储过程中的数据放入集合中。然后,您可以使用where函数来过滤结果。无需在末尾添加->get()