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",
}
]