Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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 5.2-简单选择查询返回空数组_Php_Mysql_Arrays_Eloquent_Laravel 5.2 - Fatal编程技术网

Php Laravel 5.2-简单选择查询返回空数组

Php Laravel 5.2-简单选择查询返回空数组,php,mysql,arrays,eloquent,laravel-5.2,Php,Mysql,Arrays,Eloquent,Laravel 5.2,我正在运行一个简单的查询,但由于某些原因,它没有给我正确的输出 用户应该通过复选框对一个问题给出多个答案。有了这些答案,我试着从我的数据库中获取他们的ID 以下是我正在使用的代码: foreach($foodtrucktypes as $foodtrucktype) { $type_id = DB::table('foodtruck_types') ->select('id') ->where('type', '= ', $foodtruckt

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

用户应该通过复选框对一个问题给出多个答案。有了这些答案,我试着从我的数据库中获取他们的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 />";
}
foreach($foodtrucktypes作为$foodtrucktype)
{
$type\u id=DB::table('foodtruck\u types')
->选择('id')
->其中('type','=',$foodtrucktype)
->get();
echo$foodtrucktype.“
”; 变量转储($type\u id); 回声“
”; 回声“
”; } foreach($locations作为$location) { $location\u id=DB::table('locations') ->选择('id') ->其中('name','=',$location) ->get(); echo$位置。“
”; 变量转储($location\u id); 回声“
”; 回声“
”; } foreach($partytype作为$partytype) { $party\u id=DB::table('party\u type') ->选择('id') ->其中('party_type','=',$partytype) ->get(); echo$partytype。“”; var_dump($party_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){}

Henegouwen数组(1){[0]=>object(stdClass)#195(1){[“id”]=>int(2) }}

Limburg数组(1){[0]=>object(stdClass)#196(1){[“id”]=>int(3)} }

Bedrijfsfeest数组(1){[0]=>object(stdClass)#194(1){[“id”]=> int(1)}

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

因此,代码可以工作,除了第一个foreach(foodtruck类型),它返回空数组,我不明白为什么


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

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

p可能是
'='
中的额外空间起了作用。实际上,您不需要在您只想在Laravel
->where()上进行
=
比较时指定它。如果这不起作用,请尝试
var\u dump()
变量
$foodtrucktype
首先,我建议使用雄辩的模型。。。它使编写代码变得更好

$type_id = DB::table('foodtruck_types')
    ->select('id')
    ->where('type', $foodtrucktype)
    ->get();
p可能是
'='
中的额外空间起了作用。实际上,您不需要在您只想在Laravel
->where()上进行
=
比较时指定它。如果这不起作用,请尝试
var\u dump()
变量
$foodtrucktype