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.
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"
]
]
]
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;
}
);
});
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
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.
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
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?
In a singular instance utilizing Eloquent there is
http://laravel.com/api/5.0/Illuminate/Database/Eloquent/Model.html#method_updateOrCreate
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-update-or-insert-with-laravel-4
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community