auto-injected Laravel model has no attributes

前端 未结 3 713
囚心锁ツ
囚心锁ツ 2021-01-18 13:30

I\'m new to Laravel. I have created a model, a resource controller, and a route for one of my tables, I have modified the model class to use a particular table name, but the

相关标签:
3条回答
  • 2021-01-18 13:49

    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
    
    0 讨论(0)
  • 2021-01-18 13:55

    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.

    0 讨论(0)
  • 2021-01-18 14:03

    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)
    
    0 讨论(0)
提交回复
热议问题