如何在laravel API中获取多个图像?

时间:2019-02-17 11:45:56

标签: php laravel api

我正在创建用于投诉的laravel API。此代码未在数据库中保存多个图像,并且我必须在JSON响应中以数组形式显示多个图像。我正在使用array_get,但是它对我不起作用。我尝试了很多事情,但是没有将图像保存在数据库中。我不知道。我正在将图像保存在其他表中。

public function Complains(Request $request)
  {
    $response = array();
    try {
        $allInputs = Input::all();
        $userID = trim($request->input('user_id'));
        $cordID = trim($request->input('cord_id'));
        $phone = trim($request->input('phone'));
        $address = trim($request->input('address'));
        $description = trim($request->input('description'));
     // $image = array_get($allInputs, 'image');

        $validation = Validator::make($allInputs, [
            'user_id' => 'required',
            'cord_id' => 'required',
            'phone' => 'required',
            'address' => 'required',
            'description' => 'required',
        ]);
        if ($validation->fails()) {
            $response = (new CustomResponse())->validatemessage($validation->errors()->first());
        } else {

            $checkRecord = User::where('id', $userID)->get();

            if (count($checkRecord) > 0) {

                $complainModel = new Complains();
                $complainModel->user_id = $userID;
                $complainModel->cord_id = $cordID;
                $complainModel->phone_no = $phone;
                $complainModel->address = $address;
                $complainModel->description = $description;
                $saveData = $complainModel->save();

                if ($saveData) {
                    if ($request->file('image')) {
                        $path = 'images/complain_images/';
                        // return response()->json(['check', 'In for loop']);
                        foreach ($request->file('image') as $image) {
                            $imageName = $this->uploadImage($image, $path);

                            $ImageSave = new ComplainImages();
                            $ImageSave->complain_id = $complainModel->id;
                            $ImageSave->image_url = url($path . $imageName);
                            $ImageSave->save();
                        }
                    }
                    $jsonobj = array(
                        'id' => $userID,
                        'name' => $cordID,
                        'email' => $phone,
                        'phone' => $address,
                        'description' => $description,
                    );
                    return Response::json([
                        'Exception' => "",
                        'status' => 200,
                        'error' => false,
                        'message' => "Complain Registered Successfully",
                        'data' => $jsonobj

                    ]);
                }
            }else{
                $response = (new CustomResponse())->failResponse('Invalid ID!');

            }

        }
    } catch (\Illuminate\Database\QueryException $ex) {
        $response = (new CustomResponse())->querryexception($ex);
    }
    return $response;
}

public function uploadImage($image, $destinationPath)
{
    $name = rand() . '.' . $image->getClientOriginalExtension();
    $imageSave = $image->move($destinationPath, $name);
    return $name;
}

1 个答案:

答案 0 :(得分:0)

循环allImages时出错。要保存多张图片,请尝试以下代码

foreach($request->file('image') as $image)
{
     $imageName = $this->uploadImage($image, $path);
     // other code here
}

检查您是否到达循环

return response()->json(['check': 'In for loop'])