How to make a Trait in Laravel

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
{
    protected $newDateFormat = 'd.m.Y H:i';


    // save the date in UTC format in DB table
    public function setCreatedAtAttribute($date){

        $this->attributes['created_at'] = Carbon::parse($date);

    }

    // convert the UTC format to my format
    public function getCreatedAtAttribute($date){

        return Carbon::parse($date)->format($this->newDateFormat);

    }

    // save the date in UTC format in DB table
    public function setUpdatedAtAttribute($date){

        $this->attributes['updated_at'] = Carbon::parse($date);

    }

    // convert the UTC format to my format
    public function getUpdatedAtAttribute($date){

        return Carbon::parse($date)->format($this->newDateFormat);

    }

    // save the date in UTC format in DB table
    public function setDeletedAtAttribute($date){

        $this->attributes['deleted_at'] = Carbon::parse($date);

    }

    // convert the UTC format to my format
    public function getDeletedAtAttribute($date){

        return Carbon::parse($date)->format($this->newDateFormat);

    }
}

And how to apply it for example on a User Model class...


Well, laravel follows PSR-4 so you should place your traits in:

app/Traits

You then need to make sure you namespace your trait with that path, so place:

namespace App\Traits;

At the top of your trait

Then make sure when you save the file, the file name matches the trait name. So, if your trait is called FormatDates you need to save the file as FormatDates.php

Then 'use' it in your User model by adding:

use App\Traits\FormatDates;

at the top of your model, but below your namespace

and then add:

use FormatDates;

just inside the class, so for your User model, assuming you are using the laravel default, you would get:

use App\Traits\FormatDates;

class User extends Authenticatable
{
    use Notifiable, FormatDates;

...
}

And remember to dump the autoloader:

composer dump-autoload


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:

  <?php

  namespace TraitNamespace;

  trait {{class}}
  {
   // 
  }

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