Home > Blockchain >  How to Import Products excel file to Laravel project. I have trying to more method but some issues,
How to Import Products excel file to Laravel project. I have trying to more method but some issues,

Time:08-03

I am working the project need to upload the excel file. I have trying to more method but some issues. Please help me.

Import Products - Blade Page

Product Import Page

Controller Page

  namespace App\Http\Controllers\admin;
  use App\Http\Controllers\Controller;
  use Illuminate\Http\Request;
  use Illuminate\Support\Str;
  use App\Models\Products;
  use App\Models\ProductsImage;
  use App\Models\Year;
  use App\Models\VehicleMake;
  use App\Models\VehicleModel;
  use App\Models\Category;
  use App\Models\AppSettings;
  use Hash;
  use Session;
  use File;
  use Image;

    protected function importstore(Request $request)
      { 
        $request->validate(
        [  
        'uploaded_file' => 'required|max:50000',       
        'vehicle_make'=>'required',
        'vehicle_model'=>'required',
        'category'=>'required'
        ],[
        'vehicle_make.required'=>"This is a Required filed.",
        'vehicle_model'=>"This is a Required filed.",
        'category.required'=>"This is a Required filed.",
        'uploaded_file.required'=>"This is a Required filed."
        ]);

            $uploaded_file= $request->file('uploaded_file');
            $rows = $uploaded_file->toArray();
            foreach ($rows as $key=>$row) {
              $pdct_year_from  = $row[0];
              $pdct_year_to    = $row[1];
              $pdct_title      = $row[2];
              $pdct_sku        = $row[3];
              $pdct_details    = $row[4];

              $pdct_vehicle_make  = request("vehicle_make");
              $pdct_vehicle_model = request("vehicle_model");
              $pdct_category      = request("category");

              $slug = Str::slug($product_title, '-');

        //Code for Data Save Tables
              $dataproducts = new Products();
              $dataproducts->product_year_from = $pdct_year_from;
              $dataproducts->product_year_to = $pdct_year_to;
              $dataproducts->product_make_id = $pdct_vehicle_make;
              $dataproducts->product_model_id = $pdct_vehicle_model;
              $dataproducts->product_cid = $pdct_category;
              $dataproducts->product_title = $pdct_title; 
              $dataproducts->product_details = $pdct_details;   
              $dataproducts->product_sku = $pdct_sku; 
              $dataproducts->slug = $slug;     
              $dataproducts->save();  
       
           }

    if($dataproducts){
        return back()->with('success', 'Your Product has been successfully added.');
    }else{
        return back()->with('fail', 'Somthing wrong !!!, Please try again.');
        }         
}

Error Showing: BadMethodCallException Method Illuminate\Http\UploadedFile::toArray does not exist.

CodePudding user response:

toArray() is a collection method. You cannot use it on the uploaded file. Try to

dd($request->file('uploaded_file'));

so you can iterate each row

CodePudding user response:

it's because you can't convert file to array ,if you upload csv file then you can read line by line and split each line to get data of each cell

but there is a very good package to do this stuff you can use

you can add this package to you project composer require maatwebsite/excel in this link you can see how to install this package

and you can see the documentation of how to import excel in database in just 5 min

  • Related