Advertisement
Guest User

Products Controller

a guest
Aug 23rd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.51 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use App\User;
  7. use App\Products;
  8. use App\Suppliers;
  9. use App\Category;
  10.  
  11.  
  12. class ProductsController extends Controller
  13. {
  14.  
  15.      /**
  16.      * Display a listing of the resource.
  17.      *
  18.      * @return \Illuminate\Http\Response
  19.      */
  20.     public function __construct()
  21.     {
  22.          $this->middleware('auth');
  23.          
  24.     }
  25.     /**
  26.      * Display a listing of the resource.
  27.      *
  28.      * @return \Illuminate\Http\Response
  29.      */
  30.     public function index()
  31.     {
  32.        
  33.         $products = Products::orderBy('created_at','desc')->paginate(5);        
  34.         return view('products.index')->with('products',$products);
  35.        
  36.     }
  37.  
  38.     /**
  39.      * Display a listing of the resource.
  40.      *
  41.      * @return \Illuminate\Http\Response
  42.      */
  43.     public function add()
  44.     {
  45.         $categories = Category::all();
  46.         $suppliers = Suppliers:: all();    
  47.         return view('products/add')->with('suppliers',$suppliers)->with('categories',$categories);
  48.     }
  49.  
  50.     /**
  51.      * Show the form for creating a new resource.
  52.      *
  53.      * @return \Illuminate\Http\Response
  54.      */
  55.     public function create()
  56.     {
  57.         //
  58.     }
  59.  
  60.     /**
  61.      * Store a newly created resource in storage.
  62.      *
  63.      * @param  \Illuminate\Http\Request  $request
  64.      * @return \Illuminate\Http\Response
  65.      */
  66.     public function store(Request $request)
  67.     {
  68.         $this->validate(request(), [
  69.             'suppliers' => 'required',
  70.             'category' => 'required',
  71.             'model' => 'nullable|string',
  72.             'product' => 'required|string',
  73.             'quantity' => 'required|numeric',
  74.             'price' => 'required'
  75.            
  76.         ]);
  77.  
  78.         $products = new Products;
  79.         $products->suppliers_id = $request->input('suppliers');  
  80.         $products->category_id = $request->input('category');
  81.         $products->model = $request->input('model');
  82.         $products->products_name = $request->input('product');
  83.         $products->quantity = $request->input('quantity');
  84.         $products->unit_price = str_replace(',',"", $request->input('price'));  
  85.         $products->user_id = auth()->user()->id;
  86.         $products->save();
  87.        
  88.         return redirect('/products/index')->with('success', 'Successfully add product');
  89.     }
  90.  
  91.     /**
  92.      * Display the specified resource.
  93.      *
  94.      * @param  int  $id
  95.      * @return \Illuminate\Http\Response
  96.      */
  97.     public function show($id)
  98.     {
  99.         //
  100.     }
  101.  
  102.     /**
  103.      * Show the form for editing the specified resource.
  104.      *
  105.      * @param  int  $id
  106.      * @return \Illuminate\Http\Response
  107.      */
  108.     public function edit($id)
  109.     {
  110.         $categories = Category::all();
  111.         $products = Products::find($id);    
  112.         $suppliers = Suppliers::all();              
  113.         return view('products/edit')->with('products',$products)->with('suppliers',$suppliers)->with('categories',$categories);
  114.     }
  115.      /**
  116.      * Update the specified resource in storage.
  117.      *
  118.      * @param  \Illuminate\Http\Request  $request
  119.      * @param  int  $id
  120.      * @return \Illuminate\Http\Response
  121.      */
  122.     public function update(Request $request, $id)
  123.     {
  124.         $this->validate(request(), [
  125.             'suppliers' => 'required',
  126.             'category' => 'required',
  127.             'model' => 'nullable|string',
  128.             'product' => 'required|string',
  129.             'quantity' => 'required|numeric',
  130.             'price' => 'required'
  131.            
  132.         ]);
  133.         $products = Products::find($id);  
  134.         $products->suppliers_id = $request->input('suppliers');
  135.         $products->category_id = $request->input('category');
  136.         $products->model = $request->input('model');
  137.         $products->products_name = $request->input('product');
  138.         $products->quantity = $request->input('quantity');
  139.         $products->unit_price = str_replace(',',"", $request->input('price'));            
  140.         $products->user_id = auth()->user()->id;
  141.         $products->save();
  142.        
  143.         return redirect('products/index')->with('success', 'Successfully updated product');
  144.     }
  145.  
  146.     public function search(Request $request)
  147.     {
  148.         $search = $request->input('search');
  149.         $productsearch = Products::orderBy('created_at','desc')->where('products_name', 'like', '%'. $search .'%')
  150.         ->orWhere('unit_price', 'like', '%'. $search .'%')->orWhere('model', 'like', '%'. $search .'%')
  151.         ->orWhereHas('suppliers', function($q) use($search) {
  152.             $q->where('company_name', 'like', '%' . $search . '%');
  153.         })->orWhereHas('category', function($q) use($search) {
  154.             $q->where('category', 'like', '%' . $search . '%');
  155.         })->paginate(5);
  156.         $i = 0;
  157.         foreach($productsearch as &$products){
  158.            
  159.             $product[$i] = str_replace($search,"<span class='highlight'>$search</span>",$products);
  160.             $i++;
  161.                      
  162.           }  
  163.         //   dd($products);
  164.         return view('products.index')->with('products',$product);
  165.        
  166.        
  167.     }
  168.    
  169.  
  170.     /**
  171.      * Remove the specified resource from storage.
  172.      *
  173.      * @param  int  $id
  174.      * @return \Illuminate\Http\Response
  175.      */
  176.      public function destroy($id)
  177.      {
  178.              $products = Products::find($id);
  179.              $products->delete();
  180.              return redirect('products/index')->with('products', $products)->with('success', 'Successfully deleted product');
  181.      }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement