Following this tutorial on how to make uploads in laravel, encountered the following error Class 'AppModelsFile' not found
When typing php artisan serve
in the terminal the server works fine but when you upload a file it throws the error mentioned above
I followed the steps carefully and checked File.php
, FileUpload.php
FileUpload.php
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsFile; class FileUpload extends Controller { public function createForm(){ return view('file-upload'); } public function fileUpload(Request $req){ $req->validate([ 'file' => 'required|mimes:csv,txt,xlx,xls,jpg,png,pdf|max:4096' ]); $fileModel = new File; if($req->file()) { $fileName = time().'_'.$req->file->getClientOriginalName(); $filePath = $req->file('file')->storeAs('uploads', $fileName, 'public'); $fileModel->name = time().'_'.$req->file->getClientOriginalName(); $fileModel->file_path = '/storage/' . $filePath; $fileModel->save(); return back() ->with('success','File has been uploaded.') ->with('file', $fileName); } } }
AppModelsFile.php
File.php
<?php use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class CreateFilesTable extends Migration { public function up() { Schema::create('files', function (Blueprint $table) { $table->id(); $table->string('name')->nullable(); $table->string('file_path')->nullable(); $table->timestamps(); }); } public function down() { Schema::dropIfExists('files'); } }
I get the error at line 20 in app/Http/Controllers/FileUpload
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
First, you create a model called File.
here the artisan code to create the model
php artisan make:model File -m
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0