在我的路线中,我将此路线定义为:
// 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 }}
答案 0 :(得分:3)
您不能使用id
作为数组的索引来访问具有给定ID的对象。
由于你有一个Eloquent Collection,你可以使用它的各种功能。其中一个是find()
,用于id
{{ $houses->find($characters->house_id)->name }}