61

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...

3 Answers 3

129

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

13
  • I have created the file MyTraits.php and it's in the App\Traits folder and in my User Model Class i have namespace App\Traits; use App\Traits\FormatDates; use Notifiable, SoftDeletes, FormatDates; And it doesn't work...Trait cannot be found.....if you could help me please over TeamViewer???
    – lewis4u
    Oct 27, 2016 at 15:46
  • 1
    Make sure you save the file as FormatDates.php to match your class name.
    – craig_h
    Oct 27, 2016 at 15:47
  • That fixed it....now i get another error Class '\App\User' not found..???
    – lewis4u
    Oct 27, 2016 at 15:49
  • 1
    It looks like you've added a namespace to your User model, the namespace for the User model should be namespace App;, so make sure you haven't included namespace App\Traits in your User model
    – craig_h
    Oct 27, 2016 at 15:50
  • Do you have just 5 min please i have one more problem...i can't change the locale..it is changed in config/app.php but it doesn't apply
    – lewis4u
    Oct 27, 2016 at 15:51
28

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
3
  • 1
    Thank you for the nice explanation. But I cant make it work by simply copy paste. The signature should be protected $signature = 'make:trait {name}'; and the stub should be trait {{ class }} so that the class name will be automatically replaced. Nov 26, 2020 at 10:08
  • 1
    You're welcome, I have edited the answer as you mentioned
    – Evan
    Nov 30, 2020 at 10:11
  • 1
    You should replace 'TraitNamespace' by '{{namespace}} in your stub. Dec 21, 2021 at 9:51
2

Using this package you can create create Repository, Repository with Interface, Service, Trait form command line using php artisan command.

composer require theanik/laravel-more-command --dev

For create trait

php artisan make:trait {Trait Name}

Git repo : https://github.com/theanik/laravel-more-command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.