Create a model Student with migration. Run the command-
php artisan make:model Student --migration
In students migration just add some fields for our testing.
public function up()
Schema::create('students', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 50);
$table->string('email', 50);
$table->string('phone', 20);
In
App/Providers>AppServiceProvider
use Illuminate\Support\Facades\Schema;
and in boot function
public function boot()
Schema::defaultStringLength(191);
In this view I’ve made the validations and the success messages that we can get the exact user friendly output from this. Then add the form which route is in route(‘import’). Now, the StudentController is this->
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Session;
use Excel;
use File;
class StudentController extends Controller
public function index()
return view('add-student');
public function import(Request $request){
//validate the xls file
$this->validate($request, array(
'file' => 'required'
if($request->hasFile('file')){
$extension = File::extension($request->file->getClientOriginalName());
if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {
$path = $request->file->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
$insert[] = [
'name' => $value->name,
'email' => $value->email,
'phone' => $value->phone,
if(!empty($insert)){
$insertData = DB::table('students')->insert($insert);
if ($insertData) {
Session::flash('success', 'Your Data has successfully imported');
}else {
Session::flash('error', 'Error inserting the data..');
return back();
return back();
}else {
Session::flash('error', 'File is a '.$extension.' file.!! Please upload a valid xls/csv file..!!');
return back();
For importing look at the main import function
public function import(Request $request){
//validate the xls file
$this->validate($request, array(
'file' => 'required'
if($request->hasFile('file')){
$extension = File::extension($request->file->getClientOriginalName());
if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {
$path = $request->file->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
$insert[] = [
'name' => $value->name,
'email' => $value->email,
'phone' => $value->phone,
if(!empty($insert)){
$insertData = DB::table('students')->insert($insert);
if ($insertData) {
Session::flash('success', 'Your Data has successfully imported');
}else {
Session::flash('error', 'Error inserting the data..');
return back();
return back();
}else {
Session::flash('error', 'File is a '.$extension.' file.!! Please upload a valid xls/csv file..!!');
return back();
Now take a valid xls file and upload it
The xls file is look like this-
After uploading it the screen will like-
Now go to our main database and see the data is really successfully inserted.
I'm Md Maniruzzaman Akash. I'm a problem solver and strong communicator eager to prove my value and talent at an emerging company like Revature. Motivated to advance and expand my skill set through targeted mentorship and challenging projects. I've finished my Bachelor of Computer Science and Engineering degree from Patuakhali Science and Technology University.
I'm currently working as a Full Stack Developer (PHP, Laravel, Wordpress, JavaScript, Vue JS, React, Gutenberg block) at weDevs ltd for 3+ years.
View all posts by Maniruzzaman Akash
i get error like this
Property [admin_id] does not exist on this collection instance.
Hi tnx so much your tutorial is really simply but i have a small issue this load function cant be found ….. this is the error “Call to undefined method Maatwebsite\Excel\Excel::load()”
saya memakai Database SQL Server 2016, Untuk Import Data dari File Excel ke Database SQL Server tidak bisa lebih dari 2100 Parameter.
bagaimana cara mengatasinya?
Terimaksih
Link:
https://ibb.co/WpDdFMF
I use SQL Server 2016 Database, To Import Data from Excel File into SQL Server Database, it cannot exceed 2100 Parameters.
how to handle it?
Thank you
Link :
https://ibb.co/WpDdFMF