Created
April 28, 2022 05:53
-
-
Save umaraziz0/3a44deddec3eb2cbe0398c1608ec95ac to your computer and use it in GitHub Desktop.
[Laravel] Update Image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ($request->hasFile('image')) { | |
// New image | |
$request->validate([ | |
'image' => ['image', 'mimes:jpeg,png,jpg,webp', 'max:2048'], | |
], [ | |
'image.image' => 'File harus berupa gambar.', | |
'image.mimes' => 'File harus berupa gambar.', | |
'image.max' => 'Ukuran gambar maksimal 2MB.', | |
]); | |
$fileName = $request->file('image')->hashName(); | |
$filePath = '/storage/products/' . $fileName; | |
Storage::putFileAs('public/products', $request->file('image'), $fileName); | |
Storage::delete("public/products/" . $product->image_name); | |
$product->update([ | |
'image' => $filePath, | |
'image_name' => $fileName, | |
]); | |
} | |
$product->update([ | |
'store_id' => $request->store_id, | |
'name' => $request->name, | |
'description' => $request->description, | |
'price' => $request->price, | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment