I would like to upload an image and I do not know how to create the code in laravel so I have the blade
and so the controller
To save images in laravel from a form you could use the input
tag in this way:
<input accept="image/*" type="file" name="imagen" >
the type
tag will allow us to select an image of our computer and use accept
we will be validating that they are only jpg, png type files. This for the part of the view, for the controller can be used:
$image = $request->file('imagen');
$image->move('uploads', $image->getClientOriginalName());
$nombre_tabla->imagen = image->getClientOriginalName();
This will allow us to first retrieve the file that we upload, then we will move it to the uploads folder that we will have to create in the public
folder, the getClientOriginalName function will allow us to recover the original name of a loaded file and finally we will save the address of the image in the database in a capo type string
. I hope I could answer your question
First in your form you have to put this enctype="multipart/form-data"
after your input
<input id="file-input" name="imagenesperfil" type="file"/>
then your controller here I put you divided just to upload the image
$file = $request->file('imagenperfil');
//obtenemos el nombre del archivo
$nombre = time()."_".$file->getClientOriginalName();
//indicamos que queremos guardar un nuevo archivo en el disco local
\Storage::disk('local')->put($nombre, \File::get($file));
$archivo = new Archivos;
$archivo->nombre_archivo = $nombre;
$archivo->save();
Something I recommend you use is the filesystems.php located in the config folder containing laravel so you have better control of where your files will be stored if you can see
\Storage::disk('imagenperfil')->put($nombre, \File::get($file));
where it says disk imagenprofile is why I'm referring to imageprofile within filesytem that contains this
'imgperfil' => [
'driver' => 'local',
'root' => public_path().'/proyecto/images/imagenes-perfil',
],
I hope it works for you