Laravel PHP upload csv file and import to Database

Laravel PHP upload csv file and import to Database

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 Illuminate\Database\Eloquent\Model;
  3. class Product extends Model
  4. {
  5. public $fillable = ['name','details'];
  6. }

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('expertphp-product.csv',"r");
  4.         $column=fgetcsv($fileD);
  5.         while(!feof($fileD)){
  6.          $rowData[]=fgetcsv($fileD);
  7.         }
  8.         foreach ($rowData as $key => $value) {
  9.             
  10.             $inserted_data=array('name'=>$value[0],
  11.                                  'details'=>$value[1],
  12.                             );
  13.             
  14.              Product::create($inserted_data);
  15.         }
  16.         print_r($rowData);
  17. });

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat
sohbet okey oyna
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort