Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

There is:

// The EASY way:

// adjust form to this:
{{ Form::text('first_names[][first_name]', null, array('class'=>'form-control')) }}
{{ Form::text('first_names[][first_name]', null, array('class'=>'form-control')) }}
{{ Form::text('first_names[][first_name]', null, array('class'=>'form-control')) }}
...

// then simply:
DB::table('members')->insert(Input::get('first_names'));

But query builder insert method doesn't check mass assignement constraints and it could be easily forged, so instead just a bit harder, but a BETTER way:

{{ Form::text('first_name[]', null, array('class'=>'form-control')) }}
{{ Form::text('first_name[]', null, array('class'=>'form-control')) }}
...

// then
$first_names = array_build(Input::get('first_names'), function ($key, $name) {
      return array($key, array('first_name'=>is_array($name) ? $name[0] : $name));
});
DB::table('members')->insert($first_names);

This way you make sure that only first_name field will be inserted, and all done in only single query.

Last updated 1 year ago.
0

thanks for this , i like "The EASY way" very good !

i trying someting like this and thanks for RiDdLeS


/**
* add user dependents
 */
$deps  = Input::only('full_name','gender','dep_date_birth','school','passport_no');

            $full_name = $deps['full_name'];
            $gender = $deps['gender'];
            $dep_date = $deps['dep_date_birth'];
            $school = $deps['school'];
            $passport_no = $deps['passport_no'];

            foreach( $full_name as $key => $n ) {
                DB::table('users_dependents')->insert(
                    array(
                        'full_name' => $full_name[$key],
                        'gender' => $gender[$key],
                        'dep_date_birth' => $dep_date[$key],
                        'school' => $school[$key],
                        'passport_no' => $passport_no[$key],
                        'user_id' => $getUserID
                    )
                );
            }

your way give something like this

array(5) [
    'full_names' => array(1) [
        array(1) [
            'full_name' => string (9) "sdfsdfsdf"
        ]
    ]
    'genders' => array(1) [
        array(1) [
            'gender' => string (3) "boy"
        ]
    ]
    'dep_date_births' => array(1) [
        array(1) [
            'dep_date_birth' => string (10) "53/53/4534"
        ]
    ]
    'schools' => array(1) [
        array(1) [
            'school' => string (6) "gggggg"
        ]
    ]
    'passport_nos' => array(1) [
        array(1) [
            'passport_no' => string (11) "444-444-444"
        ]
    ]
]
Last updated 1 year ago.
0

Here jQuery example if you want

    $(document).ready(function () {

        $(".add_more_dep").click(
            function(){

                var clone = $('#dependents').clone();
                clone.find("input").val("");
                clone.appendTo('#dependents:last');

                return false;
            }
        );

    });
Last updated 1 year ago.
0

The idea behind the easy way is that you can run single query for the insert query providing array of arrays, and every one of the nested arrays is a set of data to be inserted. So the example was just for a single field like in your question, but if you have more of the fields then:

// Say we want to save a Member with fields full_name, gender, school.
// you need to specify
{{ Form::text('members[0][first_name]', null, array('class'=>'form-control')) }}
{{ Form::text('members[0][gender]', null, array('class'=>'form-control')) }}
{{ Form::text('members[0][school]', null, array('class'=>'form-control')) }}

{{ Form::text('members[1][first_name]', null, array('class'=>'form-control')) }}
{{ Form::text('members[1][gender]', null, array('class'=>'form-control')) }}
{{ Form::text('members[1][school]', null, array('class'=>'form-control')) }}

...

// next no need of a foreach like in you example, simply this:

$input = Input::get('members');
DB::table('members')->insert($input); // this is going to be a batch insert, values will be send as bindings to PDO
Last updated 1 year ago.
0

im looking at this again tomorrow. i skipped it and moved to other stuff for a while after trying with no luck. thanks for all the help i really appreciate it.

Last updated 1 year ago.
0

Hi, im new on laravel, i have a question, where should i put this code? in order to make the insert into the database:

$input = Input::get('members'); DB::table('members')->insert($input); // this is going to be a batch insert, values will be send as bindings to PDO

0

jarektkaczyk said:

$input = Input::get('members');
DB::table('members')->insert($input); // this is going to be a batch insert, values will be send as bindings to PDO

but what if in such a mass insert you already have in db some values ? what would be the best way to mass insert and check for not duplicating the records?

Last updated 9 years ago.
0

In a singular instance utilizing Eloquent there is

http://laravel.com/api/5.0/Illuminate/Database/Eloquent/Model....

For bulk motions, the bottom answer in the following link, essentially states to construct a DB statement with an ON DUPLICATE KEY UPDATE statement:

http://stackoverflow.com/questions/17317193/how-can-i-use-upda...

When I'm running an edit form which may contain any number of rows, I insert a hidden field to designate those with an ID. When inserting or updating, I additionally check for ownership of rows being updated.

This also allows me to separate the entries which are updated, deleted, or inserted and deal with them differently if I desire.

0

@jarektkaczyk how to add forign key value in same table ...

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Jameron jameron Joined 16 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.