nirvair
nirvair

Reputation: 4180

Cannot save data in more than one array in Laravel

I am trying to save data in Laravel which has multiple arrays.

The array looks like this:

Array
(
    [0] => Array
        (
            [client_personnel_name] => Ron
            [client_id] => 52
            [client_personnel_email] => [email protected]
        )
    [1] => Array
        (
            [client_personnel_name] => John
            [client_id] => 52
            [client_personnel_email] => [email protected]
        )

)

When I save this data:

$personnel = ClientsPersonnel::create($client_personnel);
$personnel->save();

On debugging what data is being created to insert. This is what I get in the attributes where the sent data is stored

[attributes:protected] => Array
        (
            [updated_at] => 2015-04-23 06:53:05
            [created_at] => 2015-04-23 06:53:05
            [id] => 2
        )

How can I save the data which has multiple arrays?

Upvotes: 2

Views: 326

Answers (3)

Eng Cy
Eng Cy

Reputation: 1557

Just loop the arrays and insert seperately:

foreach ($client_personnel as $client) {
    ClientsPersonnel::create($client);
}

Upvotes: 0

Jayson
Jayson

Reputation: 1109

You can use DB::insert(), like this:

DB::table('client_personnel')->insert(array($client_personnel));

As an alternative, you could do this using loop like.

foreach ($personnels as $personnelAttributes) {
    $personnel = new ClientsPersonnel($personnelAttributes);
    $personnel->save();
}

Regards,

Upvotes: 1

Margus Pala
Margus Pala

Reputation: 8663

Laravel Eloquent does not have bulk update nor insert function. You need to create new ClientsPersonnel for each subarray and save it individually.

Upvotes: 0

Related Questions