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...
we will create one trait "ImageTrait". in that trait we will write code for image upload. So where ever we need upload image then we can use this ImageTrait trait. For example we have use profile, product picture etc, so we can use same trait method we don't require to write again and again same code.
Trait description:Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.
You can place trait any where you want. But in terms of placement you should adjust the namespace of the trait to the directory structure. For example : You want to create a Traits directory Traits >> SayHello. php .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With