如何在 Laravel 中创造特质

How to make a Trait in Laravel

如果我想在我的模型上使用这个特性,在哪里制作文件

如果我想在里面包含这个特性,这个文件应该是什么样子的:

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);

    }
}

以及如何在用户模型上应用它 class...

好吧,laravel 遵循 PSR-4 所以你应该把你的特质放在:

app/Traits

然后您需要确保使用该路径命名特征,因此放置:

namespace App\Traits;

在您的 trait

顶部

然后确保在保存文件时,文件名与特征名称相匹配。所以,如果你的特征被称为 FormatDates 你需要将文件保存为 FormatDates.php

然后 'use' 在您的用户模型中添加:

use App\Traits\FormatDates;

在您模型的顶部,但在您的 namespace

下方

然后添加:

use FormatDates;

就在 class 内部,因此对于您的用户模型,假设您使用的是 laravel 默认值,您将得到:

use App\Traits\FormatDates;

class User extends Authenticatable
{
    use Notifiable, FormatDates;

...
}

并记得转储自动加载器:

composer dump-autoload

要通过命令创建特征,我是这样做的:

  php artisan make:command TraitMakeCommand

然后你在app/Console/Commands

中有这个命令

它将创建从 Command 扩展的命令,但您必须将其更改为用于创建 类 的 GeneratorCommand,因此改为:

  use Illuminate\Console\Command;
  class TraitMakeCommand extends Command{

你应该有这个:

  use Illuminate\Console\GeneratorCommand;
  class TraitMakeCommand extends GeneratorCommand{

然后,删除 __construct() 和 handle() 方法,因为您将不需要它们。

您需要在 app/Console/Commands/stubs 中创建一个名为 traits.stub 的文件,它将成为您特征的基础:

  <?php

  namespace TraitNamespace;

  trait {{class}}
  {
   // 
  }

代码应该是这样的:

      /**
 * 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';
}

完成所有这些之后,您可以使用此命令创建特征:

php artisan make:trait nameOfTheTrait

使用此软件包,您可以使用 php artisan 命令创建存储库、带接口的存储库、服务、特征表单命令行。

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

用于创建特征

php artisan make:trait {Trait Name}

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