所以我的問題,打印結果在刀片的數據是從控制器,因此:非法串偏移和試圖獲得非對象的屬性(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