Laravel 模型:启动特征和模型

发布于 2025-01-20 03:51:55 字数 1460 浏览 0 评论 0原文

我有一些 Laravel 模型使用下一个 Trait 来调用模型的 boot 方法:

<?php

namespace App\Traits;

use Illuminate\Support\Str;

trait Uuids
{
  /**
   * Boot function from Laravel.
   */
  protected static function boot()
  {
    parent::boot();
    static::creating(function ($model) {
      if (empty($model->{$model->getKeyName()})) {
        $model->{$model->getKeyName()} = Str::uuid()->toString();

        if ($model->consecutive) {
          $model->consecutive = $model->max("consecutive") + 1;
        }
      }
    });
  }

  /**
   * Get the value indicating whether the IDs are incrementing.
   *
   * @return bool
   */
  public function getIncrementing()
  {
    return false;
  }

  /**
   * Get the auto-incrementing key type.
   *
   * @return string
   */
  public function getKeyType()
  {
    return "string";
  }
}

但是在其中一个模型上,我有一个 consecutive 列,我需要自动增量,并且我正在使用 boot 函数来做到这一点模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Order extends Model
{
  use HasFactory, Uuids;

  public static function boot()
  {
    parent::boot();
    self::creating(function ($model) {
      $model->consecutive = $model->max("consecutive") + 1;
    });
  }
}

问题是只有特征的 boot() 被调用。是否有另一种方法填充我的模型的连续列或在两个文件上调用 boot() 方法?

I have some Laravel models using the next Trait which is calling the model's boot method:

<?php

namespace App\Traits;

use Illuminate\Support\Str;

trait Uuids
{
  /**
   * Boot function from Laravel.
   */
  protected static function boot()
  {
    parent::boot();
    static::creating(function ($model) {
      if (empty($model->{$model->getKeyName()})) {
        $model->{$model->getKeyName()} = Str::uuid()->toString();

        if ($model->consecutive) {
          $model->consecutive = $model->max("consecutive") + 1;
        }
      }
    });
  }

  /**
   * Get the value indicating whether the IDs are incrementing.
   *
   * @return bool
   */
  public function getIncrementing()
  {
    return false;
  }

  /**
   * Get the auto-incrementing key type.
   *
   * @return string
   */
  public function getKeyType()
  {
    return "string";
  }
}

But on one of the models I have a consecutive column which I need to autoincrement and I was doing that with the boot function in the model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Order extends Model
{
  use HasFactory, Uuids;

  public static function boot()
  {
    parent::boot();
    self::creating(function ($model) {
      $model->consecutive = $model->max("consecutive") + 1;
    });
  }
}

The problem is that only the trait's boot() is being called. Is there another way fill the consecutive column of my model or call boot() method on both files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个少女永远怀春 2025-01-27 03:51:55

Uuids中的boot函数不会被触发。为了解决这个问题,有一个很棒的隐藏功能< Laravel 中的 /a> 表示您的特征中的函数名称是否以 boot 为前缀,然后以您的特征名称作为后缀,例如bootYourTraitName 它将使用启动模型函数启动:

<?php

namespace App\Traits;

use Illuminate\Support\Str;

trait Uuids
{
  /**
   * Boot function from Laravel.
   */
  protected static function bootUuids()
  {
    static::creating(function ($model) {
      if (empty($model->{$model->getKeyName()})) {
        $model->{$model->getKeyName()} = Str::uuid()->toString();

        if ($model->consecutive) {
          $model->consecutive = $model->max("consecutive") + 1;
        }
      }
    });
  }
}

The boot function in the Uuids will not be triggered. To fix this issue, there is an awesome hidden feature in Laravel that says if the function name in your trait is prefixed with boot and then suffixed with your Trait name like bootYourTraitName it will be booted with the boot model function:

<?php

namespace App\Traits;

use Illuminate\Support\Str;

trait Uuids
{
  /**
   * Boot function from Laravel.
   */
  protected static function bootUuids()
  {
    static::creating(function ($model) {
      if (empty($model->{$model->getKeyName()})) {
        $model->{$model->getKeyName()} = Str::uuid()->toString();

        if ($model->consecutive) {
          $model->consecutive = $model->max("consecutive") + 1;
        }
      }
    });
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文