Skip to content

Instantly share code, notes, and snippets.

@rajucs
Last active January 27, 2020 06:03
Show Gist options
  • Save rajucs/46cb8726dc0384abdbbc81a86c569b11 to your computer and use it in GitHub Desktop.
Save rajucs/46cb8726dc0384abdbbc81a86c569b11 to your computer and use it in GitHub Desktop.
Method-1:
$this->validate($request,[
'product_image' => 'mimes:jpeg,jpg,png,gif|required|max:10000'
]);
if($request->hasFile('product_image'))
{
$image_name = $request->file('product_image')->getClientOriginalName();
$filename = pathinfo($image_name,PATHINFO_FILENAME);
$image_ext = $request->file('product_image')->getClientOriginalExtension();
$fileNameToStore = $filename.'-'.time().'.'.$image_ext;
$path = $request->file('product_image')->storeAs('public/product',$fileNameToStore);
}
else{
$fileNameToStore = 'noimage.jpg';
}
$data = new Product();
$data->fill($request->all());
$data->created_by_id = Auth::user()->id;
$data->product_image = $fileNameToStore;
$data->save();
return \redirect()->back();
Method-2
$this->validate($request,[
'product_image' => 'mimes:jpeg,jpg,png,gif|required|max:10000'
]);
if($file = $request->file('product_image'))
{
$image_name = $request->file('product_image')->getClientOriginalName();
$image_ext = $request->file('product_image')->getClientOriginalExtension();
$destinationPath = public_path('images/product');
$file->move($destinationPath,$image_name);
$data['product_image'] = $image_name;
}
else{
$image_name = 'noimage.jpg';
}
$data = new Product();
$data->fill($request->all());
$data->created_by_id = Auth::user()->id;
$data->product_image = $image_name;
$data->save();
return \redirect()->back();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment