Image for post
Image for post

How to Upload pictures or Files on laravel

i am assuming you want to upload a profile picture and you are on the Register controller and you have typed the input validation

now under the validation in the store method type type this:

if (request()->has(‘avatar’)) {

// this is assuming the file input field is named avatar

$avataruploaded = request()->file(‘avatar’);

$avatarname = time() .’.’. $avataruploaded->getClientOriginalExtension();

$avatarpath = public_path(‘/profile/’); //this will create a folder named profile in your public folder

$avataruploaded->move($avatarpath, $avatarname);

return User::create([ ‘fullname’ => $data[‘fullname’],

‘level’ => $data[‘level’],

‘avatar’=> ‘/profile/’ . $avatarname, //this will movw your upload to the folder name profile in your public folder

‘email’ => $data[‘email’],

‘password’ => $data[‘password’],

]);

//if it does not have an avatar request den it will perform the operation below

}

return User::create([ ‘fullname’ => $data[‘fullname’],

‘level’ => $data[‘level’],

‘email’ => $data[‘email’],

‘password’ => $data[‘password’],

]);

}

after this in your terminal type

php artisan storage:link

Note: i made this example with picture but it also works the same for files such as pdf, docx, xlx etc

any question dont hesitate to ask in the comment section

#laravel #php #mvc #webdeveloper #code

Written by

Full Stack Developer | Laravel(PHP) | Vue Js Developer | Frontend Developer | Backend Developer | WordPress Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store