0

I am trying to use the Maatwebsite\Excel package to let users import a csv or excel file and it get imported into the DB. Also i am storing my excel sheet into the public/uploads folder and after that I am executing the Export function. You can check this logic into my controller so far my file goes into the public/uploads folder.

I keep getting the error this error .

Undefined array key 1 Please check the error image

Also, I am giving my excel sheet screenshot please check this as well.

enter image description here

Controller

public function uploadCsvQuizFile(Request $request){

        $file = $request->uploading_file;


        //dd($file);
        //Display File Name
        $file_name = $file->getClientOriginalName();
        echo '<br>';
        //Display File Extension
        echo 'File Extension: '.$file->getClientOriginalExtension();
        echo '<br>';
        //Display File Real Path
        echo 'File Real Path: '.$file->getRealPath();
        echo '<br>';
        //Display File Size
        echo 'File Size: '.$file->getSize();
        //Display File Mime Type
        echo 'File Mime Type: '.$file->getMimeType();
        //Move Uploaded File
        $destinationPath = public_path().'\uploads';
        $file->move($destinationPath,$file->getClientOriginalName());

        $data = Excel::import(new QuizImport,public_path('\uploads\file.xlsx'));

        if($data){
            return redirect()->back()->with('success','Quiz Updated Successfully');
        }else{
            return redirect()->back()->with('error','Something Went Wrong');
        }

    }

Import Class

<?php    
    namespace App\Imports;
    
    use App\Models\QuizModel;
    use Maatwebsite\Excel\Concerns\ToModel;
    
    
    class QuizImport implements ToModel
    {
        /**
        * @param array $row
        *
        * @return \Illuminate\Database\Eloquent\Model|null
        */
        public function model(array $row)
        {
    
            return new QuizModel([
                'quiz_id' => $row[0],
                'question_title' => $row[1],
                'option_a' => $row[2],
                'option_b' => $row[3],
                'option_c' => $row[4],
                'option_d' => $row[5],
                'correct_option' => $row[6],
            ]);
        }
    }

Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class QuizModel extends Model
{
    use HasFactory;
    protected $table = 'unit_quiz_questions';
    protected $fillable = ['question_id','quiz_id','question_title','option_a','option_b','option_c','option_d','correct_option','created_at','updated_at'];
}
Asad Shiekh
  • 31
  • 12
  • can you open the uploaded file in the uploads directory with excel? – Snapey Jul 11 '22 at 22:45
  • yes i can open that file as well. – Asad Shiekh Jul 11 '22 at 22:48
  • Any Idea what I am doing wrong in this code its more then 4 hours I am stuck. @Snapey – Asad Shiekh Jul 11 '22 at 22:52
  • Try to dd($row); in your import class to see what you're actually getting for data. – jkruskie Jul 11 '22 at 22:57
  • ` array:8 [▼ 0 => "quiz_id1" 1 => "question1" 2 => "option_a1" 3 => "option_b1" 4 => "option_c1" 5 => "option_d1" 6 => "correct_option1" 7 => null ] ` i am getting this after dd($row) @ImJT – Asad Shiekh Jul 11 '22 at 23:04
  • Also how can i exclude these for not inserting into database **quiz_id1 , question1 , option_a1, option_a1, option_b1, option_b1, option_d1 and correct_option** – Asad Shiekh Jul 11 '22 at 23:16
  • https://stackoverflow.com/questions/56726778/how-to-skip-first-row-when-importing-file – jkruskie Jul 12 '22 at 01:38
  • You need to skip over the headers using something like this ^ – jkruskie Jul 12 '22 at 01:38
  • Maybe it fails on the last line not the first? If the column A contains a value but the others don't. Try adding null coalesce operator to each field; 'question_title' => $row[1] ?? null; Also https://docs.laravel-excel.com/3.1/imports/model.html#skipping-rows – Snapey Jul 12 '22 at 06:53
  • You have to try this:: return new QuizModel([ 'quiz_id' => $row['quiz_id'], 'question_title' => $row['question_title'], 'option_a' => $row['option_a'], 'option_b' => $row['option_b'], 'option_c' => $row['option_c'], 'option_d' => $row['option_d'], 'correct_option' => $row['correct_option'], ]); – khush Nov 07 '22 at 05:19

0 Answers0