Error in Controller when uploading several images at once

0

I try to upload several images at the same time and uploading it to the server uploads it but only saves the name of the first one in the database, resizes it and saves it in the folder but does not insert the name in the database of data that may be due?

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');

 }
    
asked by Lendy Rodriguez Silva 26.10.2018 в 16:50
source

1 answer

1

You must also enter the

$post = Post::create($request->all());

In a foreach

    
answered by 08.11.2018 / 00:38
source