Skip to content

Instantly share code, notes, and snippets.

@Galibri
Created July 3, 2020 22:59
Show Gist options
  • Save Galibri/808d4722c08101315075a63fc7bbe5c5 to your computer and use it in GitHub Desktop.
Save Galibri/808d4722c08101315075a63fc7bbe5c5 to your computer and use it in GitHub Desktop.
<?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