Home > Software engineering >  File not found at path: file.xlsx
File not found at path: file.xlsx

Time:10-13

I'm using Maatwebsite package for inserting some data into the Database by uploading an Excel file.

So here is my routes:

Route::get('import-form', 'Olympiad\OlympiadExamExecutionController@importForm');
Route::post('import-form', 'Olympiad\OlympiadExamExecutionController@executeImport')->name('addcutsom.execution');

And here is my Controller:

use App\Imports\CustomExamImport;
use App\Olympiad\OlympiadExamExecution;
use Excel;

public function importForm()
{
    return view('import');
}

public function executeImport(Request $request)
{
    Excel::import(new CustomExamImport, $request->file);
    return "Records are imported";
}

And here is the CustomExamImport:

class CustomExamImport implements ToModel
{
    public function model(array $row)
    {
        $user_id = DB::table('members')->where('mbr_national_code', $row[3])->value('mbr_usr_id');
        return new OlympiadExamExecution([
            'oex_correct_answer_count' => $row[7],
            'oex_wrong_answer_count' => $row[8],
            'oex_no_answer_count' => $row[9],
            'oex_score' => $row[10],
        ]);
    }
} 

And this is the Blade form:

<form action="{{ route('addcutsom.execution') }}" method="POST">
    @csrf
    <label class="control-label">Upload File:</label>
    <input class="form-control" name="file" type="file">

    <button class="btn btn-primary" type="submit">ثبت</button>  
</form>

Now when I try to upload an Excel file from my computer, I get this error:

File not found at path: file.xlsx

So what's going wrong out there?

CodePudding user response:

You got the error

Call to a member function getPath() on null

because the file was not uploaded.

Change your form to inlcude enctype to upload files

<form action="{{ route('addcutsom.execution') }}" method="POST" enctype="multipart/form-data"> 

you can find mode details on the attribute here

  • Related