gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:22:23 25 4
gpt4 key购买 nike

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

用户应该通过复选框对一个问题给出多个答案。通过这些答案,我尝试从我的数据库中获取它们的 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 />";
}

这是我得到的响应:

Comfort Food array(0) { }

Desserten array(0) { }

Fastfood array(0) { }

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

Limburg array(1) { [0]=> object(stdClass)#196 (1) { ["id"]=> int(3) } }

Bedrijfsfeest array(1) { [0]=> object(stdClass)#194 (1) { ["id"]=> int(1) } }

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

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

Here's a pic of the DB

最佳答案

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

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

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

关于php - Laravel 5.2 - 简单选择查询返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36337443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com