© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•5mo ago•
6 replies
Jerome V

Why trait for auto generated column is not working?

i have this trait to generate asset_code but I got this error instead

SQLSTATE[HY000]: General error: 1364 Field 'asset_code' doesn't have a default value 
SQLSTATE[HY000]: General error: 1364 Field 'asset_code' doesn't have a default value 


The model

class Asset extends BaseModel
{
    use AssetCode;
class Asset extends BaseModel
{
    use AssetCode;


the trait

AssetCode.php
<?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());
    }
}
<?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());
    }
}
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Why selectColumn is not working
FilamentFFilament / ❓┊help
2y ago
Auto increment column
FilamentFFilament / ❓┊help
2y ago
Auto-Refresh Not Working
FilamentFFilament / ❓┊help
3y ago
reorederable column in table is not working
FilamentFFilament / ❓┊help
3y ago