Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Laravel中的WHERE返回空数组_Php_Laravel_Eloquent - Fatal编程技术网

Php 使用Laravel中的WHERE返回空数组

Php 使用Laravel中的WHERE返回空数组,php,laravel,eloquent,Php,Laravel,Eloquent,我想在拉威尔做一个WHERE。我的模型有id\u usuario,,在我的数据库中,我有五行id\u usuario=3,但Laravel只返回一个空数组 控制器 更改: Route::get('selectanimalperdido', 'AnimalPerdidoController@show 到 您需要$request->input('id') 使用以下代码作为控制器“get()如果需要数组,或first()如果需要第一个结果”: 只需在查询末尾附加“->get()”,如下所示: Ani

我想在拉威尔做一个WHERE。我的模型有
id\u usuario,
,在我的数据库中,我有五行
id\u usuario=3
,但Laravel只返回一个空数组

控制器

更改:

Route::get('selectanimalperdido', 'AnimalPerdidoController@show

您需要$request->input('id')


使用以下代码作为控制器“get()如果需要数组,或first()如果需要第一个结果”:

只需在查询末尾附加“->get()”,如下所示:

AnimalPerdido::where('id_usuario', $request->id)->get();

这是正确的-
->其中(…)
只返回一个查询生成器,要从查询生成器中实际获取结果,您需要调用
->where(…)->get()
,它将使用生成的查询实际检索结果。此外,我认为,
$request->id
不会像Eric指出的那样为您获取输入变量,您应该用
$request->get('id')
$request->input('id')
替换thast。如果他这样做,他还需要更改控制器,以接收id变量,以及更改请求url。但这在这里是不需要的。
Route::get('selectanimalperdido', 'AnimalPerdidoController@show
Route::get('selectanimalperdido/{id}', 'AnimalPerdidoController@show');
public function show(Request $request)
{
    $animaisPerdidos = AnimalPerdido::where('id_usuario', $request->id)->get();

    return response()->json($animaisPerdidos);
}
AnimalPerdido::where('id_usuario', $request->id)->get();