Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fbc's avatar
Level 2

Artisan Nested Resource Controllers Creation

Does Artisan support the creation of nested resource controllers?

I like the 'php artisan make:model -mcr' command because it's a time saver.

However it would be great if you could reference a parent resource controller to make it easier to create nested resource controller. Going back and placing the parameters into each function section in the controllers is uncomfortable.

Any ideas?

Route::resource('country', 'CountryController');
Route::resource('country.state', 'StateController');
Route::resource('country.state.county', 'CountyController');
Route::resource('country.state.county.city', 'CityController');
0 likes
7 replies
MichalOravec's avatar
Level 75
php artisan make:controller YourController -p=YourParent

Everytime when you use artisan command, you can type -h to get more info about options.

Like

php artisan make:controller -h
1 like
fbc's avatar
Level 2

So what would have been the argument for the last one? -p=country.state.county ??

php artisan make:controller -m=City -r -p=country.state.county City
MichalOravec's avatar

I try it right now and it seems to have to be a model class.

They add this ability in this commit.

fbc's avatar
Level 2

-p(for parent) is not an option for Model Class.

MichalOravec's avatar

This command

php artisan make:controller TestController -r --model=Comment --parent=Article

create this controller

<?php

namespace App\Http\Controllers;

use App\Article;
use App\Comment;
use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param  \App\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function index(Article $article)
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param  \App\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function create(Article $article)
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Article $article)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Article  $article
     * @param  \App\Comment  $comment
     * @return \Illuminate\Http\Response
     */
    public function show(Article $article, Comment $comment)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Article  $article
     * @param  \App\Comment  $comment
     * @return \Illuminate\Http\Response
     */
    public function edit(Article $article, Comment $comment)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Article  $article
     * @param  \App\Comment  $comment
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Article $article, Comment $comment)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Article  $article
     * @param  \App\Comment  $comment
     * @return \Illuminate\Http\Response
     */
    public function destroy(Article $article, Comment $comment)
    {
        //
    }
}

1 like
fbc's avatar
Level 2

yes, correct, now try to do it for parents 3 layers deep. like Country State County

It seems like it only supports 1 layer deep.

1 like
jimmyhowedotcom's avatar

@fbc Same, would be nice to do it by command, something like --model Order --parents Company,Customer

Please or to participate in this conversation.