Where to make the file if i want to use this trait on my Models
How should this file look like if i want to have this trait inside:
trait FormatDates
{
To create a trait by command I do it like this:
php artisan make:command TraitMakeCommand
then you have this command in app/Console/Commands
it will create this commands extending from Command, but you have to change it to GeneratorCommand that is the one used to create Classes, so instead:
use Illuminate\Console\Command;
class TraitMakeCommand extends Command{
you should have this:
use Illuminate\Console\GeneratorCommand;
class TraitMakeCommand extends GeneratorCommand{
then, delete __construct() and handle() methods cause you are not gonna need them.
You need to create a file in app/Console/Commands/stubs called traits.stub which is going to be the base for your trait:
and the code should be like this:
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:trait {name}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new trait';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Trait';
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__ . '/stubs/trait.stub';
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
*
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\Traits';
}
After all this, you can create a trait with this command:
php artisan make:trait nameOfTheTrait