Skip to content

Instantly share code, notes, and snippets.

@umaraziz0
Created April 28, 2022 05:53
Show Gist options
  • Save umaraziz0/3a44deddec3eb2cbe0398c1608ec95ac to your computer and use it in GitHub Desktop.
Save umaraziz0/3a44deddec3eb2cbe0398c1608ec95ac to your computer and use it in GitHub Desktop.
[Laravel] Update Image
<?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