Laravel Passport API上传多个图像

时间:2019-11-25 03:27:44

标签: php laravel postman laravel-passport

目前,我使用Laravel Passport API

上传了一张图片

我有此代码,并且工作正常。

        //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!"
        ]);

我有这个邮递员的要求来测试api,它工作正常

enter image description here

enter image description here现在,我正在尝试在一个请求中多次上传图片。此代码可能吗?

1 个答案:

答案 0 :(得分:1)

是的,可以使用您的代码完成此操作

在邮递员通行证名称中多次使用file[]

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!"
    ]);