Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anonymouse703's avatar

Auto generated field is not working.

I have this asset_code that is generated via trait but it always trigger error even if it was use in the model

error

SQLSTATE[HY000]: General error: 1364 Field 'asset_code' doesn't have a default value (Connection: mysql, SQL: insert into `assets` (`purchase_date`, `asset_type`, `category_id`, `brand_id`, `name`, `model`, `serial`, `status`, `file_id`, `created_ts`) values (2025-09-18, it_equipment, 27, 5, Macbook Pro 2020 13" 1TB, M1, X1231a1231qasdq, available, 7, 1758185039))

Model

trait

<?php

namespace App\Models\Traits;

trait AssetCode
{
    public static function bootAssetCode(): void
    {
        static::creating(function ($model) {
            if (empty($model->asset_code)) {
                $model->asset_code = self::generateAssetCode();
            }
        });
    }

    protected static function generateAssetCode(): string
    {
        return 'ASSET-' . strtoupper(uniqid());
    }
}

I'm using Laravel 12 + Filament v4

0 likes
11 replies
Merklin's avatar

That code usually goes in a model observer, not in a trait.

  • If you’re using insert() or create() with raw queries, Eloquent events are skipped, thus bootAssetCode is never fired.

  • If BaseModel has a boot method that does not call parent::boot, Laravel do not boot traits.

JussiMannisto's avatar

Where is the bootAssetCode method called? That doesn't look like any pattern I know.

Glukinho's avatar

Method boot<trait_name>() may exist in a trait and is executed the same way as booted() method of the model itself.

For example, if a model has a trait HasCustomUuid, that trait can have method bootHasCustomUuid() that can contain some logic regarding that trait (model events for example).

Glukinho's avatar

What is BaseModel? If your model extends regular \Illuminate\Database\Eloquent\Model instead, will your trait work?

It seems @merklin gave the right answer, your boot methods inheritance is broken.

anonymouse703's avatar

@merklin @glukinho

This is the BaseModel which extend the regular Model

<?php

namespace App\Models;

use App\Models\Contracts\Timestampable as TimestampableInterface;
use App\Models\Traits\Timestampable;
use Illuminate\Database\Eloquent\Model;

class BaseModel extends Model implements TimestampableInterface
{
    use Timestampable;

    public $timestamps = false;
}

Or I can't use two traits in one event?

Merklin's avatar

Can you also provide the code for creating the record that throws this error?

Glukinho's avatar

You can use any number of traits.

Your code seems ok and should work. Can you remove your BaseModel and extend your models from regular Model? Yes, you will have to implement/use Timestampable in each of your models (which is not bad as it gives you more control), but I suspect everything would work as it should.

Otherwise you should have additional code to make boot...() methods of "inherited" traits work.

anonymouse703's avatar

@merklin and @glukinho I think the bug is implementing another layer because if I do like this

class Asset extends Model  implements TimestampableInterface
{
    use AssetCode;
    use Timestampable;

it's ok

Glukinho's avatar

This is what I'm talking about. I suggest you leave it like this (no intermediate BaseModel).

Merklin's avatar

So, at the end, it was the boot() method issue. If you revert again to extend your Base Model, and in the Base Model add this:

protected static function boot(): void
    {
        parent::boot();
    }

It should work.

@glukinho a base Model could be handy sometimes. Like if you have a few properties and/or methods with the same name that can exist across a couple of models. I usually prefer Traits for such cases, but ...

1 like

Please or to participate in this conversation.