seamuswc's avatar

No idea why file is not uploading.

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.

0 likes
5 replies
Cronix's avatar

and an error is thrown

Providing the error message would be helpful

mcangueiro's avatar

Just wondering, why you have the create method on the model? What do you have on your Product controller? Do you even have a Product controller?

Sergiu17's avatar

@mcangueiro actually this is the correct way to do it, your store logic method should be either in model, either in repository class. Controllers always should be thin, they are for request - response, they don't need to know how do you save data, how do you subscribe users, how do you upload files and so one.

edoc's avatar

Do you have enctype="multipart/form-data" on your form element?

<form action="/action_page_binary.asp" method="post" enctype="multipart/form-data">
</form>
Snapey's avatar

You have overridden the create method on the model by creating your own create method? why don't you think of a different name...

Expecting to be able to call create() from within the create method of the same class is a little dumb

I certainly would not put any of this in the model

My own preference is to do stuff like this in a formRequest and do all my form handling there by creating a persist() method in the formRequest class

1 like

Please or to participate in this conversation.