Search code examples
phplaravelpostmanlaravel-passport

Laravel Passport API upload multiple images


Currently I have single upload of image using Laravel Passport API

I have this code and it's working fine.

        //Saves file to public folder
        $dateTime = date('Ymd_His');
        $file = $request->file('file');
        $fileName = $dateTime . '-' . $file->getClientOriginalName();
        $savePath = public_path('/upload/img/');
        $file->move($savePath, $fileName);

        //This saves the current file path of image to mytable
        $ActivityLog = new ActivityLogImg;
        $ActivityLog->actCode = $activity_code;
        $ActivityLog->projCode = $request->projCode;
        $ActivityLog->attachment = "/upload/img/".$fileName;
        $ActivityLog->type = "IMAGE";
        $ActivityLog->deleted = 0;
        $ActivityLog->created_by_id = Auth::user()->company_id;
        $ActivityLog->created_by_name = Auth::user()->name;
        $ActivityLog->created_at = now();
        $ActivityLog->updated_at = now();
        $ActivityLog->save();

        return response([
            "status"=>"ok",
            "message"=>"Activity successfully submitted!"
        ]);

and I have this postman request to test the api and it's working fine

enter image description here

enter image description hereNow I'm trying to do multiple upload of image in one single request. Is that possible for this code?


Solution

  • yes can do it same way with your code

    in postman pass name as file[] as multiple time

    foreach($request->file('file') as $file){
            $dateTime = date('Ymd_His');
            $fileName = $dateTime . '-' . $file->getClientOriginalName();
            $savePath = public_path('/upload/img/');
            $file->move($savePath, $fileName);
    
            //This saves the current file path of image to mytable
            $ActivityLog = new ActivityLogImg;
            $ActivityLog->actCode = $activity_code;
            $ActivityLog->projCode = $request->projCode;
            $ActivityLog->attachment = "/upload/img/".$fileName;
            $ActivityLog->type = "IMAGE";
            $ActivityLog->deleted = 0;
            $ActivityLog->created_by_id = Auth::user()->company_id;
            $ActivityLog->created_by_name = Auth::user()->name;
            $ActivityLog->created_at = now();
            $ActivityLog->updated_at = now();
            $ActivityLog->save();
    
    }
    
    return response([
            "status"=>"ok",
            "message"=>"Activity successfully submitted!"
        ]);