非法字符串偏移并尝试获取 non-object 的 属性 (laravel 5.3)
Illegal String offset & Trying to get property of non-object (laravel 5.3)
所以我无法在 blade 中打印数据来自控制器的结果,所以:
UseController.php :
$expertCategory = Category::getCategoryByID($user_id);
$data = [ 'expertCategory' => $expertCategory ];
return view('cms.user.edit', $data);
Category.php (models) getCategoryByID($user_id) return 结果数组 if 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
会return错误"Trying to get property of non-object "
如果我使用这样的代码:
@foreach($expertCategory as $expertCat)
{{ $expertCat['id'] }}
@endforeach
会 return : "Illegal string offset 'id'"
任何人都可以帮助解决这个问题:s?非常感谢!
因为 $expertCategory
是一个 dimensional array
你正面临这个问题
只需替换这个
$expertCategory = Category::getCategoryByID($user_id);
$data = [ 'expertCategory' => $expertCategory ];
return view('cms.user.edit', $data);
有
$expertCategory = Category::getCategoryByID($user_id);
$data = [ 'expertCategory' => [$expertCategory] ];
return view('cms.user.edit', $data);
然后使用
@foreach($expertCategory as $expertCat)
{{ $expertCat['id'] }}
@endforeach
在你的 blade 中它会为你工作。
所以我无法在 blade 中打印数据来自控制器的结果,所以:
UseController.php :
$expertCategory = Category::getCategoryByID($user_id); $data = [ 'expertCategory' => $expertCategory ]; return view('cms.user.edit', $data);
Category.php (models) getCategoryByID($user_id) return 结果数组 if 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
会return错误"Trying to get property of non-object "
如果我使用这样的代码:
@foreach($expertCategory as $expertCat)
{{ $expertCat['id'] }}
@endforeach
会 return : "Illegal string offset 'id'"
任何人都可以帮助解决这个问题:s?非常感谢!
因为 $expertCategory
是一个 dimensional array
你正面临这个问题
只需替换这个
$expertCategory = Category::getCategoryByID($user_id);
$data = [ 'expertCategory' => $expertCategory ];
return view('cms.user.edit', $data);
有
$expertCategory = Category::getCategoryByID($user_id);
$data = [ 'expertCategory' => [$expertCategory] ];
return view('cms.user.edit', $data);
然后使用
@foreach($expertCategory as $expertCat)
{{ $expertCat['id'] }}
@endforeach
在你的 blade 中它会为你工作。