0

I'm writing API for one mobile app. App will send multiple data with 1 or 2 images using POST method. For single data my api works without problem. I have to resize every image and store resized to thumbs folder, original to images folder of server.

Here is my code

 public function store(Request $request)
{
    $request->validate([
        'contact_id' => 'required',
        'melod_id' => 'required',
        'disease_id' => 'required',
        'treatment_id' => 'required',
        'photo' => 'required',
    ]);
    
    if ($request->has('photo')){
        $photo = $request->file('photo');
        $input['imagename'] = 'stat-'.time().'.'.$photo->extension();
        $imagename = $input['imagename'];
        $destinationPath = public_path('/thumbs');
        $img = Image::make($photo->path());
        $img->resize(150, 150, function ($constraint) {
            $constraint->aspectRatio();
        })->save($destinationPath.'/'.$imagename);
        $destinationPath = public_path('/images');
        $photo->move($destinationPath, $imagename);
        $input = $request->all();
        $input['photo'] = $imagename;
    }
    if ($request->has('photo_2')){
        $photo = $request->file('photo_2');
        $input['imagename2'] = 'shir-'.time().'.'.$photo->extension();
        $imagename2 = $input['imagename2'];
        $destinationPath = public_path('/thumbs');
        $img = Image::make($photo->path());
        $img->resize(150, 150, function ($constraint) {
            $constraint->aspectRatio();
        })->save($destinationPath.'/'.$imagename2);
        $destinationPath = public_path('/images');
        $photo->move($destinationPath, $imagename2);
        $input = $request->all();
        $input['photo'] = $imagename;
        $input['photo_2'] = $imagename2;
    }
    Statistics::create($input);

    $stat = Statistics::where('contact_id','=', $request->contact_id)->orderby('id', 'desc')->first();
    return $stat;
}

When mobile app send me multiple data in array like beloved how I can store it?

[
  {
    "contact_id": "15",
    "melod_id": "1",
    "disease_id": "1",
    "treatment_id": "2",
    "status": "1",
    "hasAphid": "0",
    "date": "2021-10-10",
    "photo": "filename1.jpg",
    "photo_2": "filename2.jpg",
},
{
    "contact_id": "15",
    "melod_id": "2",
    "disease_id": "1",
    "treatment_id": "2",
    "status": "1",
    "hasAphid": "1",
    "date": "2021-10-10"
    "photo": "filename4.jpg",
    "photo_2": "filename5.jpg",
}
]
4
  • what is your question? you mean how to do it if it has more than 2 images? Commented Jul 17, 2021 at 6:08
  • No when mobile app sends to API multiple data in array Commented Jul 17, 2021 at 6:11
  • Does this answer your question? Laravel 5: Retrieve JSON array from $request Commented Jul 17, 2021 at 6:50
  • No this is AJAX request, in my case mobile app send to api data and files in one array, and sometimes i can send me multiple arrays. Commented Jul 17, 2021 at 6:54

0

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.