Laravel:视图中的未定义偏移量index.blade.php
发布时间:2020-12-14 19:42:08 所属栏目:大数据 来源:网络整理
导读:在我的路线中,我将此路线定义为: // app/routes.phpRoute::resource('CharacterController'); 控制器中的相应方法是: // app/controllers/CharacterController.phppublic function index(){ $characters = Character::all(); $houses = House::all(); retu
在我的路线中,我将此路线定义为:
// app/routes.php Route::resource('CharacterController'); 控制器中的相应方法是: // app/controllers/CharacterController.php public function index(){ $characters = Character::all(); $houses = House::all(); return View::make('characters.index')->with(array('characters'=>$characters,'houses' => $houses)); } 最后,在视图中: // app/views/characters/index.blade.php #this fires an error: {{ $houses[$characters->house_id]->name }} # at the same time this gives correct result: {{ $houses[1]->name }} # and this IS equal to 1: {{ $characters->house_id }} 解决方法
您不能使用id作为数组的索引来访问具有给定id的对象.
由于您拥有Eloquent Collection,因此您可以使用其各种功能.其中一个是find(),用于通过id检索一个项目 {{ $houses->find($characters->house_id)->name }} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |