I try to upload several images at the same time and what is to upload it to the server uploads it but it only saves the name of the first one in the database, the others resize it and save them in the folder but it does not insert the name into the database what can be owed?
Form
{!! Form::open(array('route'=>'posts.store','method'=>'POST', 'files'=>true)) !!}
{{ Form::hidden('user_id', auth()->user()->id) }}
{{ Form::hidden('di', 'di') }}
{{ Form::label('image', 'Imagen') }}
{!! Form::file('profile_image[]', array('multiple' => true)) !!}
{{ Form::submit('Guardar', ['class' => 'btn btn-sm btn-primary']) }}
{!! Form::close() !!}
Controller
public function store(Request $request)
{
$post = Post::create($request->all());
$files = $request->file('profile_image');
foreach ((array) $files as $file)
{
if($file) {
//get filename with extension
$filenamewithextension = $file->getClientOriginalName();
//get filename without extension
$filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
//get file extension
$extension = $file->getClientOriginalExtension();
//filename to store
$filenametostore = $filename.'_'.time().'.'.$extension;
$post->fill(['file' => $filenametostore])->save();
//Upload File
$file->storeAs('public/images', $filenametostore);
$file->storeAs('public/images/mini', $filenametostore);
$file->storeAs('public/images/minin', $filenametostore);
//Resize image
$thumbnailpath = public_path('storage/images/mini/'.$filenametostore);
$img = Image::make($thumbnailpath)->resize(null, 200, function($constraint) {
$constraint->aspectRatio();
});
$img->save($thumbnailpath);
//Resize image perfil
$thumbnailpat = public_path('storage/images/minin/'.$filenametostore);
$imgn = Image::make($thumbnailpat)->resize(null, 200, function($constraint) {
$constraint->aspectRatio();
})->resizeCanvas(200, 200 );
$imgn->save($thumbnailpat);}
}
return back()->with('info', 'Foto añadida correctamente');
}
You must also enter the
in a foreach