Laravel upload csv file and import to Database – onlinecode

Laravel upload csv file and import to Database – onlinecode

In this post we will give you information about Laravel upload csv file and import to Database – onlinecode. Hear we will give you detail about Laravel upload csv file and import to Database – onlinecodeAnd how to use it also give you demo for it if it is necessary.

Laravel PHP upload csv file and import to Database

Here i will tell you that how to upload csv file from given path of system where file exist in Laravel and import in database using Laravel Eloquent.

There are so many pacages available in Laravel to import excel files in database but i am going to use PHP predefined methods or functions to read excel file line by line and save records in database.

You can click here to know more about Basic File Handling in PHP

You will learn that how to open a file, read a file, write to a file, close a file, delete a file and lastly read a file line by line by clicking above url which will help you more. So you must know about file handling in PHP.

To import csv file data in database, you must have a Laravel Eloquent Model which belons to database table.

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

  1. namespace App;
  2. use IlluminateDatabaseEloquentModel;
  3. class Product extends Model
  4. {
  5. public $fillable=['name','details'];
  6. }
namespace App;use IlluminateDatabaseEloquentModel;class Product extends Model{    public $fillable = ['name','details'];}

Now you will see how to read csv file using predefined csv reader and how to import its data in database. You can also print the data while importing csv data to database by using print_r command.

Add following line of code in your routes.php, you can put functionality in your route or you can create a function within a controller to read csv file and also importing csv file data to database its upto you.

app/Http/routes.php

  1. Route::get('read-excel',function(){
  2.     
  3.         $fileD=fopen('onlinecode-product.csv',"r");
  4.         $column=fgetcsv($fileD);
  5.         while(!feof($fileD)){
  6.          $rowData[]=fgetcsv($fileD);
  7.         }
  8.         foreach($rowDataas$key=>$value){
  9.             
  10.             $inserted_data=array('name'=>$value[],
  11.                                  'details'=>$value[1],
  12.                             );
  13.             
  14.              Product::create($inserted_data);
  15.         }
  16.         print_r($rowData);
  17. });
Route::get('read-excel',function(){			$fileD = fopen('onlinecode-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 file and import to database.

Label :

PHP

Laravel PHP Framework

Web Development

Hope this code and post will helped you for implement Laravel upload csv file and import to Database – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  47  =  49

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US