Laravel 模型:启动特征和模型
我有一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Uuids
中的boot
函数不会被触发。为了解决这个问题,有一个很棒的隐藏功能< Laravel 中的 /a> 表示您的特征中的函数名称是否以boot
为前缀,然后以您的特征名称作为后缀,例如bootYourTraitName
它将使用启动模型函数启动:The
boot
function in theUuids
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 withboot
and then suffixed with your Trait name likebootYourTraitName
it will be booted with the boot model function: