Is this what you are looking for?
$name = $this->argument('name');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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)),
]);
}
Please or to participate in this conversation.