Laravel Api – multiple images upload (to use it with angular)

  angular, api, laravel

the problem is that the json array is inserted ,but it is empty
how can I solve this problem

——————- Laravel side ————————
this is the controller function

             public function storeImages(Request $request)
            {

                $files = [];
                if($request->hasfile('images'))
                 {
        
                    foreach($request->file('images') as $file)     
                    {
        
                        $name = $file->getClientOriginalName() ;   
                        $file->move(public_path().'/images/' , $name);  
                        $files[] = $name;  
        
                    }
        
                 }
                                            
                  $file= new ImageAnnonce() ;
                  $file->annonceId = $request->annonceId;
                  $file->image_name =json_encode($files);
                  $file->save();

                  return response()->json(['message' => 'Images Uploaded Successfully']);
 
            }

this is the route api

Route::post('storeImages',[AnnonceController::class, 'storeImages']);

this is the table

    public function up()
    {
        Schema::create('image_annonces', function (Blueprint $table) {
            $table->id();
            $table->Integer('annonceId')->nullable();
            $table->String('image_name');
            $table->timestamps();
        });
    }

Source: Angular Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.