auto-injected Laravel model has no attributes

*爱你&永不变心* 提交于 2019-12-01 19:03:34

There is a naming convention on Route Model binding.

Try to change the action call to this:

public function show(Tree $category)
{
    var_dump($category);
}

Update: I looked in the source and you can also change the parameters name in resource Route declaration:

Route::resource('categories', 'CategoryController', ['parameters'=>['categories'=>'tree']]);

And use the $tree variable in action call

public function show(Tree $tree)

The issue comes in the resource routing. Replace your routing as

Route::resource('category', 'CategoryController');

Here the route name must exactly match with the controller name. That was the problem.

I also run into this problem. Laravel will follow the convention for naming the parameter.

For example your model is

AwesomeCategory

Then auto-generated edit method in AwesomeCategoryController will look like this

public function edit(AwesomeCategory $awesomeCategory) // This will not load any attributes

Change to

public function edit(AwesomeCategory $awesomecategory) // This will be automatically loaded with all attributes
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!