1

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?

1
  • What would be the question? Is that possible? Yes, it is possible. For this code? not exactly, you would have to adapt it to handle multiple files. A simple foreach($request->file('file') as $file) might be enough. In postman you can name the inputs as file[] instead file Commented Nov 25, 2019 at 3:44

1 Answer 1

1

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!"
    ]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.