【问题标题】:How to make a Trait in Laravel如何在 Laravel 中创建一个 Trait
【发布时间】:2017-03-10 09:02:39
【问题描述】:

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

如果我想在里面有这个特征,这个文件应该是什么样子:

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

    }
}

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

【问题讨论】:

    标签: laravel-5.3


    【解决方案1】:

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

    composer 需要 theanik/laravel-more-command --dev

    用于创建特征

    php artisan make:trait {特征名称}

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

    【讨论】:

      【解决方案2】:

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

        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 的文件,它将成为您的 trait 的基础:

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

      【讨论】:

      • 感谢您的精彩解释。但我不能通过简单的复制粘贴来使其工作。签名应为protected $signature = 'make:trait {name}';,存根应为特征{{ class }},以便自动替换类名。
      • 不客气,我已经编辑了你提到的答案
      • 您应该在存根中将 'TraitNamespace' 替换为 '{{namespace}}。
      【解决方案3】:

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

      app/Traits

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

      namespace App\Traits;
      

      在您的trait 顶部

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

      然后在您的用户模型中“使用”它,方法是添加:

      use App\Traits\FormatDates;
      

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

      然后添加:

      use FormatDates;
      

      就在类里面,所以对于你的 User 模型,假设你使用的是 laravel 默认值,你会得到:

      use App\Traits\FormatDates;
      
      class User extends Authenticatable
      {
          use Notifiable, FormatDates;
      
      ...
      }
      

      记得转储自动加载器:

      composer dump-autoload

      【讨论】:

      • 我创建了 MyTraits.php 文件,它位于 App\Traits 文件夹中,在我的用户模型类中,我有命名空间 App\Traits;使用 App\Traits\FormatDates;使用 Notifiable、SoftDeletes、FormatDates;而且它不起作用......无法找到特征......如果你能通过 TeamViewer 帮助我吗???
      • 确保将文件另存为 FormatDates.php 以匹配您的班级名称。
      • 修复了它....现在我得到另一个错误 Class '\App\User' not found..???
      • 看起来你已经为你的用户模型添加了一个命名空间,用户模型的命名空间应该是namespace App;,所以请确保你没有在你的用户模型中包含namespace App\Traits
      • 你只有 5 分钟吗?我还有一个问题...我无法更改语言环境..在 config/app.php 中已更改但不适用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多