5

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?

4
  • a question: can't you make consecutive column as an auto incremented column on the DB level instead of incrementing it manually in the code ? Am just being curious (if you don't mind of course). Commented Apr 8, 2022 at 20:42
  • @ths sorry for the stupid question but, is that possible if I'm using an UUID string column as primary key? Commented Apr 11, 2022 at 14:22
  • Yes, just make the auto incremented column as an index (with auto_increment as well obviously). Commented Apr 11, 2022 at 20:19
  • This question is similar to: Using traits to apply multiple model events. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented May 26 at 22:19

1 Answer 1

17

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; } } }); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.