smitsanghvi94's avatar

Eloquent Model Attribute Blank Before Processing in Laravel

I'm trying to pass an attribute (cartonsData) into my InwardTransaction model, process it inside the model, and save related details in another model (CartonTransaction). However, I noticed that cartonsData is blank before it reaches my model's saving event.

My Tinker Test


$transaction = InwardTransaction::create([
    'date' => '10/03/2025',
    'type' => \App\Enums\TransactionType::INWARDS,
    'description' => 'Random',
    'cartonsData' => [
        [
            "items" => [
                ["item_id" => "1", "quantity" => "100"],
                ["item_id" => "2", "quantity" => "100"]
            ],
            "numCartons" => "1"
        ],
        [
            "items" => [
                ["item_id" => "3", "quantity" => "1000"]
            ],
            "numCartons" => "5"
        ]
    ],
]);

echo($transaction);

Issue When I log the data inside the booted() method of my model, the cartonsData attribute is empty.

protected static function booted(): void
{
    static::saving(function (self $transaction) {
        Log::info("Data Received:", $transaction->toArray());
    });
}

Log I received:

[2025-03-24 12:07:32] local.INFO: Data Received: {"date":"2025-10-03T00:00:00.000000Z","type":"inwards","description":"Random"} 

What I Need

-I want to properly retrieve the cartonsData attribute in the model.

-Process it inside the model.

-Save the details into a related model CartonTransaction (which belongs to InwardTransaction).

What Could Be Wrong?

-Is Laravel stripping out cartonsData before it reaches the model?

-Do I need a mutator (setCartonsDataAttribute) to ensure it persists?

-Is fillable or casts affecting this behavior?

Any insights would be appreciated! Thanks.

0 likes
3 replies
tisuchi's avatar

@smitsanghvi94 Make sure your model allows cartonsData to be fillable.

e.g. In your InwardTransaction model

protected $fillable = ['date', 'type', 'description', 'cartonsData'];

And I assume it's not a custom attribute!

smitsanghvi94's avatar

@tisuchi Thanks for the response!

cartonsData is actually a custom attribute, not a column in the database. My goal is to take the data from cartonsData, process it, and break it down into multiple rows in the CartonTransaction model.

The issue is complicated because I am using Filament Forms with a Repeater component, which only allows mutating a single line into a single record, but I need to convert one line into multiple records.

Since cartonsData is not a database column, adding it to $fillable won’t help. My main problem is that Laravel seems to be stripping cartonsData before it reaches the model, as confirmed in the saving event log.

How can I make sure the attribute reaches the model so I can process it before saving related data? Also, is there a better way to handle this in Filament’s Repeater?

Would love any insights! Thanks.

Please or to participate in this conversation.