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 .
Also, I am giving my excel sheet screenshot please check this as well.
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'];
}