Search code examples
laravelfile-uploadlaravel-5.4

Laravel file upload not working, and have no idea why


MODEL:

namespace App;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;

class product extends Model
{
  public function create(Request $request) {
    $file = $request->file('photo');
    if ( $request->hasFile('photo') && $request->file('photo')->isValid() ) 
    {
      $extension = $file->extension();
      $name = 'bjdsakbhdebkhdabhkedbhe'.$extension;
      $path = $file->storeAs('public/images',$name);
    }
    else {
      return 'error';
    }

product::create([      
  'photo' => $path,    
]);

}


protected $fillable = ['name', 'price', 'roast', 'origin', 'photo', 'stock'];

}

CONTROLLER

namespace App\Http\Controllers;

 use Illuminate\Http\Request;
 use App\product;

use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;


class adminController extends Controller
{
  public function __construct() {
    $this->middleware('auth');
  }

  public function create(Request $request) {

   ini_set('max_execution_time', 300);

   $validatedData = $request->validate([
     'photo' => 'required|file|image'
    ]);

  $new = new product;
  $new->create($request);
  }


}

I am trying to upload a file image. I have reworked the above code several times and an error is thrown. Absolutely NO idea why the file is not uploading. It is not a server error. File size and time allowed have been adjusted.


Solution

  • Why are you calling product::create inside your create method in product class? This causes an infinite recursion.