Umar Akbar
Umar Akbar

Reputation: 133

Mutator is not working with bulk insertation

I am trying to insert multiple record in laravel by using Order::insert($orderArray); I have made a mutator that is

public function setOrderDetailAttribute($value)
{
    if ($value)
    {
        $this->attributes['order_detail'] = serialize($value);
    }
}

mutator is not working. But when i insret single record by using Order::create($orderArray[0][0]); Then mutator is working fine. My question how i can use mutator with insert function or bulk insertation.

Upvotes: 5

Views: 1023

Answers (2)

free2idol1
free2idol1

Reputation: 320

As @Chung has pointed out using insert() to do mass update would "just proxies the call to Query\Builder@insert() method" which is not a "pure Eloquent" way of saving records to DB.

What you need to do instead is:

foreach( $orderArray as $item ) {
     Order::create($item);
}

Upvotes: 0

Chung
Chung

Reputation: 975

When you call Order::insert($orderArray); , it doesn't touch Eloquent in fact. It just proxies the call to Query\Builder@insert() method.

So I think the mutator can't be used in that way.

Upvotes: 4

Related Questions