【发布时间】: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(食品卡车类型),它返回空数组,我不明白为什么。
【问题讨论】:
标签: php mysql arrays eloquent laravel-5.2