Why and How to Make Traits in Laravel
Using Custom Console Commands

I am a full-stack web developer that enjoys coding in Laravel and React.
Search for a command to run...
Using Custom Console Commands

I am a full-stack web developer that enjoys coding in Laravel and React.
Nice article!
So cool! Never thought of using a command to create traits. I'm wondering how would you use DayTimeTrait?
Here I will post some articles that help devs with simple and complex topics in Laravel.
Extending the Rule Object with a DateTime Trait
Switching stacks

If you don't know what Twin Macro is, check it out! It's a great library that blends Tailwind CSS and Styled Component systems, like Emotion. I love using Twin because it lets me use Tailwind while separating my styling from my markup and methods. If...

Many developers know that full-stack apps with a Vue frontend can be quickly spun up with Laravel Jetstream. What many don’t know is that recently, the Laravel team made it easy to make an Inertia app with Laravel Breeze. In this article, we'll make ...

Extending the Rule Object with a DateTime Trait

One of my favorite PHP features to use in my Laravel projects is Traits. Traits enable you to inherit multiple behaviors in PHP OOP since PHP only allows classes to inherit from one parent. I'm about to show you one of my favorite ways in which I implement them.
The Why:
use TraitName; in the class (usually on top). However, you can create your trait methods statically, so you can save a line and use your trait methods, eg. TraitName::method().The How: There's no default Artisan command to create Traits in Laravel, so you'll have to create them manually. It's as simple as creating MyTrait.php in app/Traits, and setting the namespace to namespace App\Traits. However, this can be a pain if you're creating multiple Traits, so I'll show you how I automate Trait creation.
$ php artisan make:command MakeTrait. This will create a file in app/Console/Commands/.$ php artisan stub:publish.<?php
namespace App\Traits;
trait {{trait_name}}
{
/**
* Create regular or static methods here
*/
}
MakeTrait command file. Instead of extending Command, you'll be extending GeneratorCommand. Also, you'll want to delete the __construct() and handle() methods since you're not going to need them. <?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
class MakeTrait extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:name';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
}
$signature property, make it equal to 'make:trait {name};.$description property, make it equal to 'Create a new Trait.';$type property and let it be equal to 'Trait'. This property is used to tell you if the class type has been successfully created or if it already exists.getStub() method to retrieve your stub. Also, make a getDefaultNamespace() method that will get the default namespace for the class. replaceClass($stub, $name) that will override the method that's in GeneratorCommand so that Laravel knows what to do with {{trait_name}}. Usually, it will look for DummyCommand or {{class}}. So in all, your command should look like this:<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
class MakeTrait extends GeneratorCommand
{
/**
* 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.';
protected $type = 'Trait';
protected function getStub(){
return base_path('stubs/trait.stub');
}
protected function getDefaultNamespace($rootNamespace){
return $rootNamespace . '\Traits';
}
protected function replaceClass($stub, $name){
$class = str_replace($this->getNamespace($name).'\\', '', $name);
return str_replace('{{trait_name}}', $class, $stub);
}
}
$ php artisan make:trait FooBarTrait. You'll see your new Trait in app/Traits.Conclusion:
$commands where you can place the path for a command that may not be found by Laravel. By default, it will look directly in the Commands folder, as you may see in the commands() method below.DayTimeTrait. In a future article, I'll show you an advanced usage with Rules.