Laravel excel import

By ukmodak | March 31st 2024 10:38:02 AM | viewed 931 times

Run the following command to install excel library

D:\xampp_726\htdocs\ptm> composer require maatwebsite/excel
OR
D:\xampp_726\htdocs\ptm> composer require "maatwebsite/excel":"~2.1.0"

Now open config/app.php file and add service provider and aliase.

config/app.php

'providers' => [
	....
	Maatwebsite\Excel\ExcelServiceProvider::class,
],

'aliases' => [
	....
	'Excel' => Maatwebsite\Excel\Facades\Excel::class,
],

D:\xampp_726\htdocs\ptm> php artisan config:cache
Import excel of csv file

Add script to any controller to get excel data as follows:

<?php namespace App\Http\Controllers\Ecommerce;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use Auth;
use Carbon\Carbon;
use Session;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\File;
use Excel;

class EcExcelFileUploadController extends Controller {

   public function employeeUpload(Request $request){
	   
	    ini_set('max_execution_time', 100000);
        $request->validate([
            'employeeExcell' => 'required'
        ]);


        $path = $request->file('employeeExcell')->getRealPath();
		
		$insert = null;
		$insertUserMember = null;
		$insertPin = null;
		
        $data = Excel::load($path)->get();
		
		try{
		
				if(!empty($data) && $data->count()){
					foreach ($data as $key => $v){
					  $fulname = @explode(" ",@$v['user_full_name'],2);
						if(!empty($v)){
						
								 $existing = User::where('mobile',@$v['contact_no'])->where('email',@$v['email_id'])->first();
							
									if(empty(@$existing->id)){
													  
										$userIns = new User();         

										$userIns->first_name = @$fulname[0];
										$userIns->middle_name = '';
										$userIns->last_name = @$fulname[1];
										$userIns->user_name = @$v['user_name'];
										$userIns->mobile=@$v['contact_no'];
										$userIns->email = @$v['email_id'];
										$userIns->password =bcrypt('123456');
										$userIns->remember_token ='';
										$userIns->create_date = @Carbon::now();
										$userIns->active =1;
										$userIns->meta_name =@explode("@",@$v['email_id'])[0].rand(1000,9999);
										$userIns->user_group_meta_name ='retailer';
										$userIns->business_type_meta_name ='';
										$userIns->business_name_meta_name ='';
										$userIns->boff =0;
										$userIns->provider ='';
										$userIns->provider_id ='';
										$userIns->birth_date ='';
										$userIns->sex ='';
										$userIns->prefer_contact ='';
										$userIns->emp_desg=@$v['designation'];
										$userIns->emp_dept=@$v['department'];
										
										$insertPin =rand(1000,9999);
																		
											$insert = $userIns->save();
											
											
											 if($insert){
												 
												 $membertype = EcMemberType::where('title','Employee')->first();
			
													$ecMembetUser = new EcMemberUser();

													$ecMembetUser->user_meta_name= $userIns->meta_name;
													$ecMembetUser->ec_member_type_id = $membertype->id;
													$ecMembetUser->is_active= 1;
													$ecMembetUser->date= @Carbon::now();
													
													$insertUserMember = $ecMembetUser->save();

													$ecEmployeePin = new EcEmployeePin();
													
													$ecEmployeePin->user_id =$userIns->id; 
													$ecEmployeePin->ec_member_type_id =$membertype->id;
													$ecEmployeePin->pin = $insertPin;
													$ecEmployeePinInsert = $ecEmployeePin->save();
													
													
													 if($ecEmployeePinInsert){

													  $this->mailClass->employeeRegMail($userIns,'123456',$insertPin);

													}

											 }
			  
									}else{
													  		  
										$userIns = @$existing;
										$userIns->first_name = @$fulname[0];
										$userIns->middle_name = '';
										$userIns->last_name = @$fulname[1];
										$userIns->user_name = @$v['user_name'];
										$userIns->mobile=@$v['contact_no'];
										$userIns->email = @$v['email_id'];
										$userIns->password =bcrypt('123456');
										$userIns->remember_token ='';
										$userIns->create_date = @Carbon::now();
										$userIns->active =1;
										$userIns->meta_name =@explode("@",@$v['email_id'])[0].rand(1000,9999);
										$userIns->user_group_meta_name ='retailer';
										$userIns->business_type_meta_name ='';
										$userIns->business_name_meta_name ='';
										$userIns->boff =0;
										$userIns->provider ='';
										$userIns->provider_id ='';
										$userIns->birth_date ='';
										$userIns->sex ='';
										$userIns->prefer_contact ='';
										$userIns->emp_desg=@$v['designation'];
										$userIns->emp_dept=@$v['department'];
										
										$insertPin =rand(1000,9999);
																		
											  $insert = $userIns->save();											 						
												 if($insert){
													 
													 $membertype =EcMemberType::where('title','Employee')->first();
				
														$ecMembetUser = @EcMemberUser::where('user_meta_name',@$userIns->meta_name)->first();
														 
														if($ecMembetUser){
															
															    //$ecMembetUser->user_meta_name= $userIns->meta_name;
																$ecMembetUser->ec_member_type_id = $membertype->id;
																$ecMembetUser->is_active= 1;
																$ecMembetUser->date= @Carbon::now();
																$insertUserMember = $ecMembetUser->save();

																$ecEmployeePin = EcEmployeePin::where('user_id',$userIns->id)->first();
																//$ecEmployeePin->user_id =$userIns->id; 
																$ecEmployeePin->ec_member_type_id =$membertype->id;
																$ecEmployeePin->pin = $insertPin;
																$memberPointIns = $ecEmployeePin->save();
																
																
																 
																if($memberPointIns){

																  $this->mailClass->employeeRegMail($userIns,'123456',$insertPin);

																}
															
														}else{
															$ecMembetUser = new EcMemberUser();

																$ecMembetUser->user_meta_name= $userIns->meta_name;
																$ecMembetUser->ec_member_type_id = $membertype->id;
																$ecMembetUser->is_active= 1;
																$ecMembetUser->date= @Carbon::now();
																
																$insertUserMember = $ecMembetUser->save();

																$ecEmployeePin = new EcEmployeePin();
																
																$ecEmployeePin->user_id =$userIns->id; 
																$ecEmployeePin->ec_member_type_id =$membertype->id;
																$ecEmployeePin->pin = $insertPin;
																
																$ecEmployeePinInsert = $ecEmployeePin->save();
																
																
																 if($ecEmployeePinInsert){

																  $this->mailClass->employeeRegMail($userIns,'123456',$insertPin);

																}
														}

														

												 }
													  
												  }
							 
									  }
						}
				   
				}		 
					if(!empty($insert)){
							return redirect()->route('excelup/index')->with('success','Data insert successfull');  
						
					}else{
					   return view('ecommerce.ecExcelFileUpload.employeeUpload');
					}
			
	    }catch(Exception $e) {
          echo 'Message: ' .$e->getMessage();
        }		
	}
}
Export as excel or csv file

bONEandALL
Visitor

Total : 26654

Today :3

Today Visit Country :

  • France
  • United States