Home > Back-end >  How To upload multiple pictures With laravel and Cloudinary?
How To upload multiple pictures With laravel and Cloudinary?

Time:03-23

Actually I'm on a laravel and reactJs project. I want to upload multiple pictures (album) with laravel and Cloudinary but I didn't find the right solution.

This my db migration file:

  public function up()
    {
        Schema::create('unites', function (Blueprint $table) {
            $table->increments('unit_id');
            $table->unsignedInteger('building_id');
            $table->foreign('building_id')->references('building_id')->on('buildings');
            $table->unsignedInteger('floor_id');
            $table->foreign('floor_id')->references('floor_id')->on('floors');     
            $table->string('unit_name',200);
            $table->tinyinteger('unit_status');
            $table->integer('unit_roomnumber');
            $table->string('unit_type',5);
            $table->string('unit_pictures');
            $table->date('unit_added_date')->format("yyyy-MM-dd");
             $table->timestamps();
        });
    }

An this My function in the controller :


        else {
            $unit = new Unite();
            $unit ->unit_name = $request->input('unit_name');
            $unit->building_id = $request->input('building_id');
            $unit->floor_id = $request->input('building_id');
            $unit->unit_type = $request->input('unit_type');
            $unit->unit_status = $request->input('unit_status');
            $unit->unit_roomnumber = $request->input('unit_roomnumber');
            $unit->unit_added_date = $request->input('unit_added_date');
            $images = $request->file('unit_pictures');
            $uploaded = [];
            foreach ($images as $image) {
                error_log('for statement fires.');
                Cloudinary::upload($image, null, [
                    'folder' => '/units',
                   // 'discard_original_filename' => true,
                ])->getSecurePath();
               return array($uploaded);
    
            }
           $unit->unit_pictures=$request->file('unit_pictures');
    
            $unit->save();
            return response()->json([
                'status' => 200,
            ]);

I would be very Thankful if anyone of you can help me

CodePudding user response:

foreach ($request->file('images') as $imagefile) {
  $uploadedFileUrl = Cloudinary::upload($imagefile->getRealPath())->getSecurePath();
}

CodePudding user response:

The Cloudinary::upload() allows you to process a single asset at one time (see Upload API). For every upload, the response JSON will be returned containing all the information for the newly uploaded asset (see sample response JSON). If you wish to capture the results of every asset, you could collect them within the foreach, for example:

$unit->unit_pictures = $request->file('unit_pictures'); 

$allUploadApiReponse = array();

foreach ( $unit->unit_pictures=$request->file('unit_pictures') as $imagefile) 
{ 
    $uploadedFileUrl = Cloudinary::upload($imagefile->getRealPath(), [ 'folder' => 'Units' ] )->getSecurePath(); 
    array_push($allUploadApiReponse, $uploadedFileUrl);
} 

// To check content
print_r($allUploadApiReponse);

$unit->save();`
  • Related