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

ioanandrei's avatar

How to use artisan command arguments in stub file

I created an artisan command to generate repositories files. The result of my command should be a generated file with some default methods as find(), getAll() and so on.

My command is like this: php artisan make:repository { repositoryRoute } { targeted model path }.

The command file looks like this:

class makeRepository extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:repository {name} {model}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a repository';

    protected $type = 'class';

    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return base_path().'/stubs/repository.stub';
    }

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Repositories';
    }
    
    protected function getArguments()
    {
        return [
            ['name', InputArgument::REQUIRED, 'The name and root of the file.'],
            ['model', InputArgument::REQUIRED, 'The name and root of the model class targeted.'],
        ];
    }
}

and the sub file looks like this:

<?php

use Illuminate\Support\Collection;

class {{ class }}
{
    /**
     * Store the entity
     *
     * @return bool
     */
    public function store() : bool
    {
        //
    }

    /**
     * Find an entity based on id.
     *
     * @return {{ model }}
     */
    public function find(int $id) : ?{{ model }}
    {
        //
    }

    /**
    * Get all the entities
    *
    * @return Illuminate\Support\Collection
    */
    public function getAll(): ?Collection
    {
        return {{ model }}::all();
    }
}

My problem is that i can't figure it out how to use the model variable in the stub file. When i use the command, the name variable will work just fine, but the model variable will not be replaced.

I tried to look in some of the default methods like generateController and I saw this method, but I can't figure it out how to use it in my case.


    protected function buildModelReplacements(array $replace)
    {
        $modelClass = $this->parseModel($this->option('model'));

        if (! class_exists($modelClass)) {
            if ($this->confirm("A {$modelClass} model does not exist. Do you want to generate it?", true)) {
                $this->call('make:model', ['name' => $modelClass]);
            }
        }

        return array_merge($replace, [
            'DummyFullModelClass' => $modelClass,
            '{{ namespacedModel }}' => $modelClass,
            '{{namespacedModel}}' => $modelClass,
            'DummyModelClass' => class_basename($modelClass),
            '{{ model }}' => class_basename($modelClass),
            '{{model}}' => class_basename($modelClass),
            'DummyModelVariable' => lcfirst(class_basename($modelClass)),
            '{{ modelVariable }}' => lcfirst(class_basename($modelClass)),
            '{{modelVariable}}' => lcfirst(class_basename($modelClass)),
        ]);
    }
0 likes
9 replies
bugsysha's avatar

Is this what you are looking for?

$name = $this->argument('name');
bugsysha's avatar

In the handle method of the command. Have you looked at the documentation? Everything is very well explained.

ioanandrei's avatar

The only thing i found in the documentation about the sub files is how you can publish them.

Can you give me a link?

bugsysha's avatar

You are building your own command which will create PHP classes from stubs. You are providing the name and other stuff from the terminal. What I've posted shows you how to get the value of the passed argument within your command. Then you need to grab that stub and replace placeholders with correct values and save it.

This is how you handle arguments https://laravel.com/docs/7.x/artisan#arguments

The rest of what you are trying to do is done by your code/command.

ioanandrei's avatar

"Then you need to grab that stub and replace placeholders with correct values and save it."

How can i do this? Grab the value from command and replace placeholders with correct values and save it.

bugsysha's avatar

I've shown how you can grab the value, and you can replace it with str_replace().

lionixteam's avatar

If anyone still needs this.

@ioanandrei

You actually need to overwrite buildClass() method from GeneratorCommand and do your modifications there.

In my case I needed to apply "type" into the .stub file, so I did the following:

protected function buildClass($name): string
    {
        $stub = $this->files->get($this->getStub());

        return $this->replaceNamespace($stub, $name)->replaceClass(
            $this->replaceType($stub, $this->getNameInput()),
            $name
        );
    }

protected function replaceType($stub, $name): string
    {
        $type = strtolower(str_replace('Component', '', $name));

        return str_replace(['{{ type }}', '{{type}}'], $type, $stub);
    }
3 likes
MannyFay's avatar

Hi @ioanandrei :) Why do you use protected $type = 'class'; in your command file? I see nowhere in your files from the first post where you use it..

Regards, Manny

Please or to participate in this conversation.