gpt4 book ai didi

mysql - 如何在 Laravel 中将数组中的元素存储到变量中?

转载 作者:行者123 更新时间:2023-11-29 09:25:20 25 4
gpt4 key购买 nike

我想将“区域”存储在 $school_info 数组$area 变量中。使用 $area 我希望将更多 3 行存储到 3 个不同的数组中,例如:$school_info1$school_info2$school_info3 >.

public function schooldetailviewid($id)
{
$school_id = $id;
$school_info = DB::table('school_infos')->where('school_id', '=', $school_id)->get();
$area = $school_info->area;

// recomendation start
$school_info1 = DB::table('school_infos')->where('area', '=', $area)->get();
$school_info2 = DB::table('school_infos')->where('area', '=', $area)->get();
$school_info3 = DB::table('school_infos')->where('area', '=', $area)->get();
// recomendation end

return view('schooldetail', compact('school_info', 'school_info1', 'school_info2', 'school_info3'));
}

问题:元素存储不起作用,并且未获取另外 3 行!我正在尝试制作一个简单的推荐系统。

错误

Exception Property [area] does not exist on this collection instance.

最佳答案

.get() 返回一个对象数组,使用 .first() 仅获取一个对象:

$school_info = DB::table('school_infos')->where('school_id', '=', $school_id)->first();
$area = $school_info->area;

否则,您需要访问数组的第一个元素:

$school_info = DB::table('school_infos')->where('school_id', '=', $school_id)->get();
$area = $school_info[0]->area;

关于mysql - 如何在 Laravel 中将数组中的元素存储到变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59696874/

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