在Laravel中使用WHERE返回空数组

时间:2018-12-01 15:30:22

标签: php laravel eloquent

我正在尝试在Laravel中进行WHERE。我的模型有id_usuario,,在我的数据库中,我有id_usuario = 3有五行,但是Laravel只返回一个空数组。

控制器

<?php

public function show(Request $request)
{
    $animaisPerdidos = AnimalPerdido::where('id_usuario', $request->id);

    return response()->json($animaisPerdidos);
}

路线

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

在邮递员中

enter image description here

模型

class AnimalPerdido extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    protected $table = 'animais_perdidos';

    protected $fillable = [
        'id', 'id_usuario', 'lat', 'lng', 'id_tipo_pet', 'raca', 'id_sexo', 'data', 'possui_identificador', 'id_cores', 'informacoes_adicionais', 'nome_identificador', 'foto'
    ];  

}

4 个答案:

答案 0 :(得分:2)

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

public function show(Request $request)
{
    $animaisPerdidos = AnimalPerdido::where('id_usuario', $request->id)->get();

    return response()->json($animaisPerdidos);
}

答案 1 :(得分:0)

更改:

this.data = {
  periods: [
    {
      duration: "PT0H0M12.500S",
      baseUrl:
        "URL",
      adaptionSets: [
        {
          segmentAlignment: true,
          maxWidth: 1280,
          maxHeight: 720,
          maxFrameRate: 24,
          par: "16:9",
          lang: "und",
          representations: [{
            id="1",
            mimeType="video/mp4",
            codecs="avc1.4d401f",
            width="1280",
            height="720",
            frameRate="24",
            sar="1:1",
            startWithSAP="1",
            bandwidth="2412988",
            segmentList: [{
                timescale="24000",
                duration= "96000",
                sourceURL: "segment_init.mp4",
                SegmentURL: [
                    {media: "segment_1.m4s"},
                    {media: "segment_2.m4s"},
                    {media: "segment_3.m4s"},
                    {media: "segment_4.m4s"}
                ]
            }
          ]
        }]
        }
      ]
    }
  ]
};

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

答案 2 :(得分:0)

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

here

答案 3 :(得分:0)

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

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