Logo

Office Address

123/A, Miranda City Likaoli
Prikano, Dope

Phone Number

+0989 7876 9865 9

+(090) 8765 86543 85

Email Address

info@example.com

example.mail@hum.com

Upload CSV files and import to Database using Laravel PHP

Upload CSV files and import to Database using Laravel PHP

In this article, we are how to upload CSV file from a system and import in the database using Laravel.

There are countless bundles accessible in Laravel to import dominant documents in the data set yet I will utilize PHP predefined techniques or capacities to peruse dominant document line by line and save records in the information base. 

You can click here to find out about Basic File Handling in PHP 

You will discover that how to open a document, read a record, keep in touch with a document, close a record, erase a record and ultimately read a recorded line by line by tapping the above URL which will help you more. So you should think about documents dealing with PHP.

To import CSV file data in the database, you must have a Laravel Model which belongs to a database table.

First, create a product table in the database and then create a model file for the product table in the following path app/Product.php and put the following line of code.


Model 


use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
    public $fillable = ['name','details'];
}

Presently you will perceive how to peruse CSV document utilizing predefined CSV peruser and how to import its information in the data set. You can likewise print the information while bringing in CSV information to the data set by utilizing print_r order. 

Add following line of code in your routes.php, you can place usefulness in your course or you can make a capacity inside a regulator to peruse CSV record and furthermore bringing in CSV document information to the data set it's upto you.

app/Http/routes.php

Route::get('read-excel',function(){
    
        $fileD = fopen('expertphp-product.csv',"r");
        $column=fgetcsv($fileD);
        while(!feof($fileD)){
         $rowData[]=fgetcsv($fileD);
        }
        foreach ($rowData as $key => $value) {
            
            $inserted_data=array('name'=>$value[0],
                                 'details'=>$value[1],
                            );
            
             Product::create($inserted_data);
        }
        print_r($rowData);
});

 
Now you can try this code in your application to read CSV files and import to the database the same method you can use in other PHP related frameworks also.

Tags

  • Import CSV data to MySQL database with laravel

  • laravel 5.6 import CSV to the database

  • laravel 5.8 import CSV to the database

  • laravel import large CSV file
  • import excel file in laravel
  • laravel CSV import package

  • laravel read CSV file to the array

  • bulk upload in laravel

Related Posts

How to Disable Mouse Right Click using jQuery

How to Disable Mouse Right Click using jQuery

To Prevent Content theft right-click disable players a mail role this is the common issue faced by website and blog owners. This can be controlled by disabling right-click on websites. Disabling mouse

Read More
How to identify the best WordPress developer?

How to identify the best WordPress developer?

Having a strong and attractive WordPress website is essential for businesses to succeed online in the digital-first world of today. However, picking the best WordPress developer might be difficult. Wi

Read More
Optimizing for Voice Search: A Complete SEO Guide

Optimizing for Voice Search: A Complete SEO Guide

Voice search is becoming a reality that is influencing how people use the internet, not just a sci-fi idea. Voice search is changing digital habits, from using Google Assistant to get weather updates

Read More