gpt4 book ai didi

laravel - 尝试使用laravel获取非对象的属性 'catid'

转载 作者:行者123 更新时间:2023-12-02 15:05:07 24 4
gpt4 key购买 nike

这是我的代码

public function edit($id) {
$categories = Category::all();
$category = Category::find($id);
return view('categories.edit', compact('categories', 'category'));

}

以下是我的编辑表单

{!! Form::open(['url' => ['categories', $category->catid], 'method' => 'put']) !!}
<div class="form-group padding-t-20 {{ $errors->has('name') ? 'has-error' : '' }}">
{{ Form::label('name', 'Name') }}
{{ Form::text('name', $category->name, ['class' => 'form-control', 'placeholder' => 'Category']) }}
<span class="text-danger">{{ $errors->has('name') ? $errors->first('name') : '' }}</span>
</div>
<div class="text-center padding-t-20">
{{ Form::submit('Update', ['class' => 'btn btn-info btn-fill btn-wd']) }}
</div>
{!! Form::close() !!}

它工作正常,但是当我使用如下所示的数据库外观时

 public function edit($id) {
$categories = DB::select('select * from tbl_menu_category');
$category = DB::select('select * from tbl_menu_category where catid = ?', [$id]);
return view('categories.edit', compact('categories', 'category'));
}

尝试获取非对象的属性“catid”时出现错误

最佳答案

当您使用 DB::select() 查询数据库时,即使您试图从数据库中仅获取记录,它也会以数组形式返回结果。

所以解决方案是:

public function edit($id) {
$categories = DB::select('select * from tbl_menu_category');
$category = DB::select('select * from tbl_menu_category where catid = ?', [$id])[0]; <--- grab the first index of the array returned.
return view('categories.edit', compact('categories', 'category'));
}

希望对你有帮助:)

关于laravel - 尝试使用laravel获取非对象的属性 'catid',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51906576/

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