I'm trying to create an register form that includes an image upload. When I submit the form, it stores an "/private/var/tmp/" path for the image on database and the image is not stored anywhere in the public folder. I don't know what I might be doing wrong. Please help.
My config/filesystems:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
My controller CreateNewUser:
class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;
protected function validator(array $input)
{
return Validator::make( $input, [
'firstname' => ['required', 'string', 'max:255'],
'lastname' => ['required', 'string', 'max:255'],
'email' => [
'required',
'string',
'email',
'max:255',
Rule::unique(User::class),
],
'mobile' => ['required', 'int'],
'country' => ['required', 'string', 'max:255'],
'password' => $this->passwordRules(),
'db_upload' => ['required', 'image'] //This the image I want to upload
]);
}
public function register(Request $request)
{
$this->validator($request->all())->validate();
$imageUrl = $this->storeImage($request);
$input = $request->all();
$input['db_upload'] = $imageUrl;
$user = $this->create($input);
$this->guard()->login($user);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
protected function storeImage(Request $request) {
$path = $request->file('db_upload')->store('public/uploads');
return substr($path, strlen('public/'));
}
public function create(array $input)
{
$user = User::create([
'firstname' => $input['firstname'],
'lastname' => $input['lastname'],
'email' => $input['email'],
'mobile' => $input['mobile'],
'country' => $input['email'],
'password' => Hash::make($input['password']),
'db_upload' => $input['db_upload'],
]);
$user->notify(new WelcomeEmailNotification());
return $user;
}
}
question from:
https://stackoverflow.com/questions/65862100/cant-store-image-upload-on-public-folder-laravel 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…