I have an application in which I need to add products that contain more than one image and I already tried the code and it worked well on Postman, but when I use it on flutter it gives me an error **
foreach() argument must be of type array|object, null given
** and I cannot solve the problem
I need to upload more than one image to laravel api
my laravel code
public function add(Request $request){
$attars = $request->validate([
'name' => 'required|string',
'desc' => 'required|string',
'status' => 'required|integer',
'ad' => 'required|integer|max:1',
'top' => 'required|integer|max:1',
'phoneNumber' => 'required|string',
'socialMdeia' => 'required|string',
'webLink' => 'required|string',
'show' => 'required|integer|max:1',
]);
$imagesName = [];
if($request->has('images')){
foreach($request->file('images') as $image){
$imageName = $image->getClientOriginalName().'-image-'.time().rand(1,1000).'.'.$image->extension();
$image->move(public_path('product_images'),$imageName);
$imagesName[] = $imageName;
}
}
$imagesData = json_encode($imagesName);
$post = productes::create([
'image' => $imagesData,
'name' => $attars['name'],
'desc' => $attars['desc'],
'status' => $attars['status'],
'ad' => $attars['ad'],
'top' => $attars['top'],
'phoneNumber' => $attars['phoneNumber'],
'socialMdeia' => $attars['socialMdeia'],
'webLink' => $attars['webLink'],
'show' => $attars['show'],
]);
return response([
'message' => 'productes Created',
'post' => $post
], 200);
}
flutter api services
Future<ApiResponse> addProductes(
var image,
String name,
String desc,
String status,
String ad,
String top,
String phoneNumber,
String socialMdeia,
String webLink,
String show) async {
ApiResponse apiResponse = ApiResponse();
final response =
await http.post(Uri.parse('${addProductesLink}'), headers: {
'Accept': 'application/json'
}, body: {
'images[]': image,
'name': name,
'desc': desc,
'status': status,
'ad': ad,
'top': top,
'phoneNumber': phoneNumber,
'socialMdeia': socialMdeia,
'webLink': webLink,
'show': show
});
switch (response.statusCode) {
case 200:
apiResponse.data = productesModel.fromJson(jsonDecode(response.body));
break;
case 422:
final errors = jsonDecode(response.body)['errors'];
apiResponse.error = errors[errors.keys.elementAt(0)][0];
break;
case 403:
apiResponse.error = jsonDecode(response.body)['message'];
break;
default:
apiResponse.error = jsonDecode(response.body)['message'];
}
return apiResponse;
flutter controller upload images
uploadImage(context) async {
FilePickerResult? result = await FilePicker.platform.pickFiles(
allowMultiple: true,
);
List<File> file = result!.paths.map((path) => File(path!)).toList();
images.addAll(file);
if (images != null) {
for (File file in images) {
imageList.add(file.path);
}
print(imageList);
} else {}
flutter controller send data
Future addProductesData(image, name, desc, status, ad, top, phoneNumber,
socialMdeia, webLinks, show) async {
FormData formData = FormData({});
for (String path in imageList) {
formData.files.add(MapEntry(
"images[]",
MultipartFile(File(path),
filename:
'${DateTime.now().microsecondsSinceEpoch}.${path.split('.').last}')));
}
ApiResponse response = await addProductesServices().addProductes(image,
name, desc, status, ad, top, phoneNumber, socialMdeia, webLinks, show);
if (response.error != null) {
print(response.error);
}
postman