
How to insert multiple rows in mysql using loop in laravel?
How to insert multiple rows in mysql using loop in laravel?
In this session, we will try our hand at solving the "How to insert multiple rows in mysql using loop in laravel?".
You can insert multiple rows in MySQL using foreach loop in laravel. You can get the records (values) in an array and you can insert the value in the table using foreach iteration-
Insert multiple records using foreach in laravel
$timezones = ['(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi', '(UTC-12:00) International Date Line West', 'UTC-11:00) Midway Island, Samoa'] ; foreach($timezones as $timezone){ Timezone::create([ 'name' => $timezone, ]); } //Import Timezone model before class definition use App\Models\Timezone;
This code snippet example is helpful when you want to insert multiple records in a single execution.
-
Insert multiple records with multiple columns in table using laravel eloquent
//Controller's method to insert records in the table public function insertSalaryRecords(){ $emp_details = array( array("emp_id" => 4, "emp_name"=> 'samarjeet kumar', "basic_sal" => 21000), array("emp_id" => 5, "emp_name"=> 'sonu chauhan', "basic_sal" => 25000), array("emp_id" => 6, "emp_name"=> 'manoj singh', "basic_sal" => 27000) ); $new_insert_array=array(); foreach($emp_details as $key=>$val) { $new_insert_array[]=array('emp_id'=>$val['emp_id'],'emp_name'=>$val['emp_name'],'basic_sal'=>$val['basic_sal']); } Salary::insert($new_insert_array); echo "data inserted"; } //Create model and table structure using migration command php artisan make:model Salary -m //Migration file content - database/migrations/<2021_08_04_115521>_create_salaries_table.php Schema::create('salaries', function (Blueprint $table) { $table->id(); $table->integer('emp_id'); $table->string('emp_name'); $table->bigInteger('basic_sal'); $table->timestamps(); }); //Create salary controller php artisan make:controller SalaryController //RouteĀ Route::get('/insert-salaries', 'SalaryController@insertSalaryRecords');
You can insert multiple records with multiple columns in table using laravel eloquent
-
Upsert() method in laravel
DB::table('users')->upsert([ ['id' => 1, 'email' => '[email protected]'], ['id' => 2, 'email' => '[email protected]'], ], ['email']);
The first argument is the values to insert/update the records and the second argument is the column(s) that uniquely identify records. All databases except SQL Server require these columns to have a PRIMARY or UNIQUE index.
It will insert the record if the value does not already exist in the database else it will update the records. The
upsert
method will automatically set thecreated_at
andupdated_at
timestamps if timestamps are enabled on the model.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- Extract only time from datetime in laravel
- Non-static method App\Http\Helper::myFunction() should not be called statically
- Get current month records in laravel 7/8
- Insert values in pivot table dynamically in laravel
- Call to a member function pluck() on array
- Remove public from url in laravel project
- Update if exist else insert new record in laravel
- Composer create project laravel/laravel example app
- Laravel specific table Migration
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied
- Method Illuminate\Http\Request::validated does not exist
- How to increment column value of table in Laravel
- How to get data from two tables in laravel
- How to get user information using hootlex/laravel-friendships package in laravel
- Update last created record in Laravel
- On delete set foreign id column value null using migration in laravel 8
- Route prefix with auth middleware in laravel
- Comment .env file in laravel
- Define variable and use in Laravel controller method
- Remove array keys and values if it does not exist in other array in Laravel
- Trying to get property 'title' of non-object
- How to add class to tr in table using foreach in laravel
- Create project factory and seed data in laravel
- PhpMyAdmin - Error The mysqli extension is missing