Root Zanli
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
o5t6x7pgljbm
/
public_html
/
app
/
Http
/
Controllers
/
Admin
/
Filename :
ProductController.php
back
Copy
<?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Models\Product; use App\Models\ProductCategory; use App\Models\Group; use App\Models\ProductImage; use Auth; use Hash; use Session; use DB; use Socialite; use Image; Use Response; use Helpers; use File; class ProductController extends Controller { public function index() { $products = Product::get(); return view('Admin.Product.index',compact('products')); } public function product_list(Request $request) { $columns = array("product_type","group_name","product_category_name","product_name","product_image","product_sp","expires_on","product_id"); $columns2 =array("products.product_type","groups.group_name","product_categories.product_category_name","products.product_name","products.product_image","products.product_sp","products.expires_on","products.product_id"); $totalData = Product::count(); $limit = $request->input('length'); $start = $request->input('start'); $dir = $request->input('order.0.dir'); $query = Product::select($columns2) ->leftjoin('groups','groups.group_id','=','products.group_id') ->leftjoin('product_categories','product_categories.product_category_id','=','products.product_category_id'); $products = $query->get(); $totalFiltered = $products->count(); $products = $query->offset($start); $products = $query->limit($limit); if(isset($columns2[$request->input('order.0.column')])){ $order = $columns[$request->input('order.0.column')]; $products = $query->orderBy($order,$dir); } if(isset($request->sort_by) && $request->sort_by == 1) $products = $query->orderBy('products.created_at', 'asc'); else $products = $query->orderBy('products.created_at', 'desc'); $products = $query->get(); //$totalFiltered = $query->count(); $data = array(); if(!empty($products)) { foreach ($products as $key => $product) { $edit = route('products.edit',$product->product_id); $delete = route('products.destroy', $product->product_id); foreach ($columns as $key => $column) { if ($column == 'product_id') { $nestedData[$column] = "<form action='{$delete}' method='POST' id='productTable-".$product->$column."'> <input name='_method' type='hidden' value='DELETE'> <a href='{$edit}' title='EDIT' class='mb-6 btn-floating waves-effect waves-light gradient-45deg-green-teal gradient-shadow'> ".csrf_field()." <i class='material-icons'>edit</i></a> <button type='button' title='DELETE' class='mb-6 btn-floating waves-effect waves-light gradient-45deg-purple-deep-orange gradient-shadow' onclick='delete_product(".$product->$column.");'> <i class='material-icons'>clear</i> </button> </form>"; } else if($column == 'created_at') { $nestedData[$column] = date('j M Y h:i a',strtotime($product->$column)); }else if($column == 'product_image') { $nestedData[$column] = '<img src="'.asset($product->product_image).'" width="65px"/>'; } else if($column == 'expires_on') { $nestedData[$column] = date('d M Y',strtotime($product->$column)); } else { $nestedData[$column] = $product->$column; } } $data[] = $nestedData; } } //echo count($data);exit; $json_data = array( "draw" => intval($request->input('draw')), "recordsTotal" => intval($totalData), "recordsFiltered" => intval($totalFiltered), "data" => $data ); // echo '<pre>';print_r($json_data);die; echo json_encode($json_data); } public function create() { $products = Product::get(); $groups = Group::get(); $product_categories = ProductCategory::get(); return view('Admin.Product.create',compact('products','groups','product_categories')); } public function store(Request $request) { $validate = $request->validate([ 'product_type' => 'required', 'group_id' => 'required', 'product_category_id' => 'required', 'product_name' => 'required', 'product_sp' => 'required', 'expires_on' => 'required', ]); $request['expires_on'] = date("Y-m-d", strtotime($request['expires_on'])); if($request->hasFile('productImage')) { $files = $request->file('productImage'); foreach($files as $key => $file){ $imageName = rand().time().'.'.$file->extension(); // Save thumbnail image $img = Image::make($file->getRealPath()); $img->resize(100, 100, function($constraint){ $constraint->aspectRatio(); })->save(public_path('/product_images/thumb/').$imageName); //Save original size image $file->move(public_path('product_images'), $imageName); ProductImage::insert([ 'product_id' => $products->product_id, 'image_name' => $imageName ]); if($key == 0){ Product::where('product_id','=',$products->product_id)->update([ 'product_image' => $imageName, ]); } } } $products = Product::create($request->all()); return redirect()->route('products.index')->with('success','Record Successfully Inserted.'); } public function edit($id) { $products = Product::find($id); $groups = Group::get(); $product_categories = ProductCategory::get(); $productImages = ProductImage::where('product_id',$id)->get(); return view('Admin.Product.edit',compact('products','groups','productImages','product_categories')); } public function update(Request $request,$id) { $validate = $request->validate([ 'product_type' => 'required', 'group_id' => 'required', 'product_category_id' => 'required', 'product_name' => 'required', 'product_sp' => 'required', 'expires_on' => 'required', ]); $products = Product::find($id); if($request->hasFile('productImage')) { $files = $request->file('productImage'); foreach($files as $key => $file){ $imageName = rand().time().'.'.$file->extension(); // Save thumbnail image $img = Image::make($file->getRealPath()); $img->resize(100, 100, function($constraint){ $constraint->aspectRatio(); })->save(public_path('/product_images/thumb/').$imageName); //Save original size image $file->move(public_path('product_images'),$imageName); DB::table('product_images')->insert([ 'product_id' => $id, 'image_name' => $imageName ]); } } $request['expires_on'] = date("Y-m-d", strtotime($request['expires_on'])); $products->update($request->all()); return redirect()->route('products.index')->with('success','Record Updated Successfully.'); } public function destroy($id) { $data = Product::find($id)->delete(); return redirect()->route('products.index')->with('success','Record Deleted Successfully'); } public function delete_img(Request $request) { $productImageId = $request->productImageId; // $productImageName = $request->productImageName; ProductImage::where('product_image_id',$productImageId)->delete(); // File::delete(public_path('/product_images/thumb/'.$productImageName)); // File::delete(public_path('/product_images/'.$productImageName)); } }