【问题标题】:Laravel 5.2 - Simple Select Query returns empty arrayLaravel 5.2 - 简单选择查询返回空数组
【发布时间】:2016-03-31 15:17:41
【问题描述】:

我正在运行一个简单的查询,但由于某种原因它没有给我正确的输出。

用户应该通过复选框对一个问题给出多个答案。有了这些答案,我尝试从我的数据库中获取它们的 ID。

这是我正在使用的代码:

foreach($foodtrucktypes as $foodtrucktype)
{
    $type_id = DB::table('foodtruck_types')
        ->select('id')
        ->where('type', '= ', $foodtrucktype)
        ->get();
    echo $foodtrucktype."<br />";
    var_dump($type_id);
    echo "<br />";
    echo "<br />";
}

foreach($locations as $location)
{
    $location_id = DB::table('locations')
        ->select('id')
        ->where('name', '=', $location)
        ->get();

    echo $location."<br />";
    var_dump($location_id);
    echo "<br />";
    echo "<br />";
}

foreach($partytypes as $partytype)
{
    $party_id = DB::table('party_types')
        ->select('id')
        ->where('party_type' ,'=', $partytype)
        ->get();

    echo $partytype."<br :>";
    var_dump($party_id);
    echo "<br />";
    echo "<br />";
}

这是我得到的回复:

舒适食品数组(0) { }

甜点数组(0) { }

快餐数组(0) { }

Heenegouwen array(1) { [0]=> object(stdClass)#195 (1) { ["id"]=> int(2) } }

林堡数组(1) { [0]=> object(stdClass)#196 (1) { ["id"]=> int(3) } }

Bedrijfsfeest 数组(1) { [0]=> 对象(stdClass)#194 (1) { ["id"]=> int(1) } }

Communiefeest 数组(1) { [0]=> object(stdClass)#197 (1) { ["id"]=> int(2) } }

所以代码有效,除了第一个 foreach(食品卡车类型),它返回空数组,我不明白为什么。

Here's a pic of the DB

【问题讨论】:

    标签: php mysql arrays eloquent laravel-5.2


    【解决方案1】:

    首先,我真的建议使用 Eloquent 模型……它使编写代码变得更好。

    $type_id = DB::table('foodtruck_types')
        ->select('id')
        ->where('type', $foodtrucktype)
        ->get();
    

    也许'= ' 中的额外空间产生了影响。您实际上不需要在您只想对 Laravel -&gt;where() 进行 = 比较时指定它。如果这不起作用,请尝试var_dump() $foodtrucktype 变量?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      • 2021-09-02
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多