如何在Laravel中制作特质

lew*_*s4u 20 laravel-5.3

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

如果我想在内部使用这个特性,这个文件应该怎么样:

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

    }
}
Run Code Online (Sandbox Code Playgroud)

以及如何在用户模型类中应用它...

cra*_*g_h 61

好吧,laravel跟随,PSR-4所以你应该把你的特点放在:

app/Traits

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

namespace App\Traits;
Run Code Online (Sandbox Code Playgroud)

在你的顶部 trait

然后确保在保存文件时,文件名与特征名称匹配.因此,如果调用了特征,则FormatDates需要将文件另存为FormatDates.php

然后通过添加以下内容在"用户"模型中"使用"它:

use App\Traits\FormatDates;
Run Code Online (Sandbox Code Playgroud)

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

然后添加:

use FormatDates;
Run Code Online (Sandbox Code Playgroud)

就在类中,所以对于你的用户模型,假设你使用了laravel默认值,你会得到:

use App\Traits\FormatDates;

class User extends Authenticatable
{
    use Notifiable, FormatDates;

...
}
Run Code Online (Sandbox Code Playgroud)

并记得转储自动加载器:

composer dump-autoload


Eva*_*van 16

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

  php artisan make:command TraitMakeCommand
Run Code Online (Sandbox Code Playgroud)

那么你在 app/Console/Commands 中有这个命令

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

  use Illuminate\Console\Command;
  class TraitMakeCommand extends Command{
Run Code Online (Sandbox Code Playgroud)

你应该有这个:

  use Illuminate\Console\GeneratorCommand;
  class TraitMakeCommand extends GeneratorCommand{
Run Code Online (Sandbox Code Playgroud)

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

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

  <?php

  namespace TraitNamespace;

  trait {{class}}
  {
   // 
  }
Run Code Online (Sandbox Code Playgroud)

代码应该是这样的:

      /**
 * 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';
}
Run Code Online (Sandbox Code Playgroud)

之后,您可以使用以下命令创建特征:

php artisan make:trait nameOfTheTrait
Run Code Online (Sandbox Code Playgroud)