所以我的问题,打印结果在刀片的数据是从控制器,因此:非法串偏移和试图获得非对象的属性(laravel 5.3)
UseController.php:
$expertCategory = Category::getCategoryByID($user_id); $data = [ 'expertCategory' => $expertCategory ]; return view('cms.user.edit', $data);
Category.php(型号)getCategoryByID($ USER_ID)返回结果数组,如果I DD(expertCategory);在控制器,其结果是:
array:9 [▼ "id" => 1 "name" => "Beauty" "sequence" => 1 "background_color" => "ffffff" "status" => "Active" "created_at" => "2017-06-19 09:41:38" "updated_at" => "2017-06-19 09:41:38" "icon_filename" => "beauty-icon" "iconURL" => array:3 [▼ "small" => "http://localhost:8000/images/category_icons/small/beauty-icon" "medium" => "http://localhost:8000/images/category_icons/medium/beauty-icon" ] ]
但是,当我想用的foreach的结果blade.php与代码打印:
@foreach($expertCategory as $expertCat) {{ $expertCat->id }} @endforeach
将返回错误“试图让非对象“
的属性如果我使用这样的代码:
@foreach($expertCategory as $expertCat) {{ $expertCat['id'] }} @endforeach
则回复:“非法串偏移‘身份证’”
有人能帮助解决这个问题:■?非常感谢 !
不要做foreach,它只是'$ expertCategory [“id”]'。毕竟这是一个单一的类别。 – apokryfos
[PHP:“注意:未定义的变量”,“注意:未定义的索引”和“注意:未定义的偏移量”的可能重复](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable- notice-undefined-index-and-notice-undef) –
谢谢先生,这真的很有用,并且只是意识到我的查询使用“first()”,这就是为什么每次使用时都会出错。感谢你的帮助!上帝保佑! – Axel