How to Create Multiple Records in Laravel?
Oct 04, 2022
Hello Friends,
In this article we will cover how to implement how to create multiple records in laravel. This article goes into detail on laravel creating multiple records. if you have questions about laravel insert multiple records eloquent then I will give a simple example with a solution. I’m going to show you about laravel insert multiple records. So, let's follow a few steps to create an example of laravel's eloquent insert of multiple rows.
If we work on a big project and then we may be required to add multiple rows to a database using laravel eloquent. Laravel provides an insert method for bulk records created on DB.
In the below example you can see I use a multidimensional $myItems array variable that inserts multiple records same time using DB::insert(). So let's see and try this.
You can use this example with the versions of laravel 6, laravel 7, laravel 8, and laravel 9.
Example:
we will create MultipleRecordController. so you can see the below code with output:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class MultipleRecordController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function multiplerecords()
{
$myItems = [
['title'=>'HD Topi','description'=>'NiceSnippets.com'],
['title'=>'HD Topi 2','description'=>'NiceSnippets.com 2'],
['title'=>'HD Topi 3','description'=>'NiceSnippets.com 3']
];
DB::table("items")->insert($myItems);
}
}
It will help you...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial