Save Multiple Data Once into Database in Laravel
Hi Dave I think you want to do something like this
$data = $request->all();
$finalArray = array();
foreach($data as $key=>$value){
array_push($finalArray, array(
'fltno'=>$value['sflt'],
'model'=>$value['smodel'],
'engine'=>$value['sengine'],
'loc'=>$value['sloc'],
'serviceType'=>$value['sstye'],
'nextSvr'=> $value['snsvr'] )
);
});
Model::insert($finalArray);
This will submit all data. I am assuming you are getting array in request with these keys.
Hope this will help.
How to save multiple records in Laravel
public function store(Request $request)
{
$data = $request->all();
$mark = new Record();
$mark->out_of = $data['out_of'];
if ($data['student_id']) {
foreach ($data['student_id'] as $row => $value) {
$data1 = array(
'out_of' => $mark->out_of,
'student_id' => $data['student_id'][$row],
'guardian_id' => $data['guardian_id'][$row],
'marks_obtained' => $data['marks_obtained'][$row],
'comment' => $data['comment'][$row],
);
Record::create($data1);
}
}
return redirect('/home')->with('success', 'Record Added Successfully');
}
in your view
<form action="/record/store" method="post">
@csrf
<input type="text" name="student_id">
<input type="text" name="guardian_id[]">
<input type="text" name="marks_obtained[]">
<input type="text" name="out_of">
<input type="text" name="comment[]">
<button type="submit">Post</button>
</form>
Bulk Insertion in Laravel using eloquent ORM
You can just use Eloquent::insert()
.
For example:
$data = [
['name'=>'Coder 1', 'rep'=>'4096'],
['name'=>'Coder 2', 'rep'=>'2048'],
//...
];
Coder::insert($data);
Laravel save multiple record at once
If someone have this problem i just solved by implode and explode $request, and then loop with a for cicle. Quite easy.
function postSave( Request $request)
{
$rules = $this->validateForm();
$validator = Validator::make($request->all(), $rules);
if ($validator->passes()) {
$idz = implode(",", $request->input('destination'));
$ids = explode(",", $idz);
$tgz = implode(",", $request->input('target'));
$tgs = explode(",", $tgz);
for ($i = 0; $i < count($ids); $i++)
{
$pipeline = new Pipeline;
$pipeline->destination = $ids[$i];
$pipeline->target = $tgs[$i];
$pipeline->save();
$i = $i++;
}
How to insert multiple rows from a single query using eloquent/fluent
It is really easy to do a bulk insert in Laravel using Eloquent or the query builder.
You can use the following approach.
$data = [
['user_id'=>'Coder 1', 'subject_id'=> 4096],
['user_id'=>'Coder 2', 'subject_id'=> 2048],
//...
];
Model::insert($data); // Eloquent approach
DB::table('table')->insert($data); // Query Builder approach
In your case you already have the data within the $query
variable.
Save multiple data in database column from select multiple
If you want to store multiple university data for one student first of all you need 3 table, students(so your Seller Info), universities and seller_info_university(will be pivot table),
seller_info_university Migration must be like;
Schema::create('seller_info_university', function (Blueprint $table) {
$table->bigInteger('seller_info_id')->unsigned();
$table->bigInteger('university_id')->unsigned();
});
Add this code your SellerInfo Model;
public function universities()
{
return $this->belongsToMany(University::class); // Your University model
}
Add this code your University Model;
public function students()
{
return $this->belongsToMany(SellerInfo::class); // Your Student(SellerInfo) model
}
In your Controller try this
$student = Seller_info::create([
'name' => $request['name'],
'email' => $request['email'],
'passport' => $request['passport'],
'phone_number' => $request['phone_number'],
'address' => $request['address'],
'dateofbirth' => $request['dateofbirth'],
'ielts' => $request['ielts'],
'openingcharge' => $request['openingcharge'],
'serviceCharge' => $request['serviceCharge'],
'applydate' => $request['applydate'],
'visaStatus'=> $request['visaStatus'],
'country' => $request['country'],
// 'university'=> $request['university'], you dont need there anymore
]);
$student->universities()->attach($request['universities']); // You can use attach(), detach() or sync() functions, you can search in Laravel Documentation
return back();
And View;
<select class="selectpicker" multiple data-live-search="true" name="universities[]">
@foreach($districts as $row)
<option value= {{$row->name}} > {{$row->name}} </option>
@endforeach
</select>
Related Topics
Secure Hash and Salt For PHP Passwords
How to Calculate the Difference Between Two Dates Using PHP
How to Sort a Multi-Dimensional Array by Value
How to Sanitize User Input With PHP
How to Pass JavaScript Variables to PHP
How Should a Model Be Structured in MVC
How to Flatten a Multidimensional Array
Are Pdo Prepared Statements Sufficient to Prevent SQL Injection
How to Remove Duplicate Values from a Multi-Dimensional Array in PHP
How to Use Return Inside a Recursive Function in PHP
Warning: Preg_Replace(): Unknown Modifier
PHP Multidimensional Array Search by Value
Pass a PHP String to a JavaScript Variable (And Escape Newlines)
Returning Json from a PHP Script
Getting Check Box Values as Checked from Database Codeigniter