2

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?

1
  • loop the array and save seperately Commented Apr 23, 2015 at 7:07

3 Answers 3

1

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,

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

Just loop the arrays and insert seperately:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.