How to Upload pictures or Files on laravel
Hello Everyone, This is my first Medium Post and i hope it can help someone,
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