ctyler
283
15
Eloquent

Nested Resource Controllers

Posted 2 months ago by ctyler

I am having difficulty with nested resource controllers. If I have a resource controller:

Route::resource('coursecat.course', 'CourseController');

That give me these routese (via php artisan route:list) I get the following routes:

admin                                                    |                          | Closure                                                                     | web                                 |
|        | POST      | admin/coursecat                                          | coursecat.store          | App\Http\Controllers\Admin\[email protected]                        | web                                 |
|        | GET|HEAD  | admin/coursecat                                          | coursecat.index          | App\Http\Controllers\Admin\[email protected]                        | web                                 |
|        | GET|HEAD  | admin/coursecat/create                                   | coursecat.create         | App\Http\Controllers\Admin\[email protected]                       | web                                 |
|        | PUT|PATCH | admin/coursecat/{coursecat}                              | coursecat.update         | App\Http\Controllers\Admin\[email protected]                       | web                                 |
|        | GET|HEAD  | admin/coursecat/{coursecat}                              | coursecat.show           | App\Http\Controllers\Admin\[email protected]                         | web                                 |
|        | DELETE    | admin/coursecat/{coursecat}                              | coursecat.destroy        | App\Http\Controllers\Admin\[email protected]                      | web                                 |
|        | POST      | admin/coursecat/{coursecat}/course                       | coursecat.course.store   | App\Http\Controllers\Admin\[email protected]                           | web                                 |
|        | GET|HEAD  | admin/coursecat/{coursecat}/course                       | coursecat.course.index   | App\Http\Controllers\Admin\[email protected]                           | web                                 |
|        | GET|HEAD  | admin/coursecat/{coursecat}/course/create                | coursecat.course.create  | App\Http\Controllers\Admin\[email protected]                          | web                                 |
|        | DELETE    | admin/coursecat/{coursecat}/course/{course}              | coursecat.course.destroy | App\Http\Controllers\Admin\[email protected]                         | web                                 |
|        | PUT|PATCH | admin/coursecat/{coursecat}/course/{course}              | coursecat.course.update  | App\Http\Controllers\Admin\[email protected]                          | web                                 |
|        | GET|HEAD  | admin/coursecat/{coursecat}/course/{course}              | coursecat.course.show    | App\Http\Controllers\Admin\[email protected]                            | web                                 |
|        | GET|HEAD  | admin/coursecat/{coursecat}/course/{course}/edit         | coursecat.course.edit    | App\Http\Controllers\Admin\[email protected]                            | web                                 |
|        | GET|HEAD  | admin/coursecat/{coursecat}/edit                         | coursecat.edit           | App\Http\Controllers\Admin\[email protected]

In my CourseController, for my index() action, I have:

public function index(CourseCat $courseCat)
    {
        return view('admin.course.index', compact('courseCat'));
    }

In my admin.course.index view I have

{{ dd($courseCat) }}

So, if I visit http://10.0.0.2/admin/coursecat/10/course I would expect to see the output of CourseCat object with the ID of 10. However, it is returning:

CourseCat {#458 ▼
  +sortable: array:2 [▶]
  #fillable: array:9 [▶]
  #table: "course_categories"
  #connection: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

I am not sure why that is not working. Does route model binding not work with nested controllers. Is there something I need to do special for nested controllers.

Any help would be appreciated.

Please sign in or create an account to participate in this conversation.