'Flutter Laravel API update user
I tried to make crud, so how to update a user without changing his photo because if I pass a photo the update works perfectly but if I didn't pass the photo I got Exception: Invalid image data and the value of photo change in database.
Here is my Laravel code
$product=Products::findOrFail($id);
$validator = Validator::make($request->all(), [
'name' => 'required',
'price'=>'required',
'detail' => 'required',
'quantity' => 'required',
'discount' => 'required'
]);
$input = $request->all();
if(!is_null($input['photo'])){
$image = $input['photo'];
$image = str_replace('data:image/png;base64,', '', $image);
$image = str_replace(' ', '+', $image);
$imageName = Str::random(50).'.'.'png';
if(!is_dir('/uploads/products')) {
mkdir('/uploads/products', 0755, true);
}
Storage::disk('public_uploads')->delete($product->photo);
Storage::disk('public_uploads')->put($imageName, base64_decode($image));
$product->photo = $imageName;
$product->name = $input['name'];
$product->price = $input['price'];
$product->detail = $input['detail'];
$product->quantity = $input['quantity'];
$product->update();
Solution 1:[1]
If statement should be like this
if($request->hasFile('photo')){
//
}
instead of this
if(!is_null($input['photo'])){
//
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pradeep |
