Created
July 3, 2020 22:59
-
-
Save Galibri/808d4722c08101315075a63fc7bbe5c5 to your computer and use it in GitHub Desktop.
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 | |
/********************************************************************** | |
** The following will store the image name only. | |
**********************************************************************/ | |
/** | |
* Storing images | |
* The input field name is 'image' here, you can change with yours | |
*/ | |
public function store(Request $request) { | |
$product = new Product; | |
if($request->has('image')) { | |
$image = $request->file('image'); | |
$image_name = time() . '_' . rand(100, 999) . '_' . $image->getClientOriginalName(); | |
$image->move(public_path('uploads/images'), $image_name); | |
$product->image = $image_name; | |
} | |
} | |
/** | |
* Updating image and deleting the exisint one | |
* The input field name is 'image' here, you can change with yours | |
*/ | |
public function update(Request $request, Product $product) { | |
if($request->has('image')) { | |
if($product->image) { | |
$upload_path = 'uploads/images/'; | |
File::delete($upload_path . $product->image); | |
} | |
$image = $request->file('image'); | |
$image_name = time() . '_' . rand(100, 999) . '_' . $image->getClientOriginalName(); | |
$image->move(public_path('uploads/images'), $image_name); | |
$product->image = $image_name; | |
} | |
} | |
/********************************************************************** | |
** The following will store the image name and upload path in the db | |
**********************************************************************/ | |
/** | |
* Storing images | |
* The input field name is 'image' here, you can change with yours | |
*/ | |
public function store(Request $request) { | |
$product = new Product; | |
if($request->has('image')) { | |
$image = $request->file('image'); | |
$path = 'uploads/images'; | |
$image_name = time() . '_' . rand(100, 999) . '_' . $image->getClientOriginalName(); | |
$image->move(public_path($path), $image_name); | |
$product->image = $path . '/' . $image_name; | |
} | |
} | |
/** | |
* Updating image and deleting the exisint one | |
* The input field name is 'image' here, you can change with yours | |
*/ | |
public function update(Request $request, Product $product) { | |
if($request->has('image')) { | |
if($product->image) { | |
File::delete($product->image); | |
} | |
$image = $request->file('image'); | |
$path = 'uploads/images'; | |
$image_name = time() . '_' . rand(100, 999) . '_' . $image->getClientOriginalName(); | |
$image->move(public_path($path), $image_name); | |
$product->image = $path . '/' . $image_name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment