Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\User;
- use App\Products;
- use App\Suppliers;
- use App\Category;
- class ProductsController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function __construct()
- {
- $this->middleware('auth');
- }
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- $products = Products::orderBy('created_at','desc')->paginate(5);
- return view('products.index')->with('products',$products);
- }
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function add()
- {
- $categories = Category::all();
- $suppliers = Suppliers:: all();
- return view('products/add')->with('suppliers',$suppliers)->with('categories',$categories);
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- //
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- $this->validate(request(), [
- 'suppliers' => 'required',
- 'category' => 'required',
- 'model' => 'nullable|string',
- 'product' => 'required|string',
- 'quantity' => 'required|numeric',
- 'price' => 'required'
- ]);
- $products = new Products;
- $products->suppliers_id = $request->input('suppliers');
- $products->category_id = $request->input('category');
- $products->model = $request->input('model');
- $products->products_name = $request->input('product');
- $products->quantity = $request->input('quantity');
- $products->unit_price = str_replace(',',"", $request->input('price'));
- $products->user_id = auth()->user()->id;
- $products->save();
- return redirect('/products/index')->with('success', 'Successfully add product');
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- //
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit($id)
- {
- $categories = Category::all();
- $products = Products::find($id);
- $suppliers = Suppliers::all();
- return view('products/edit')->with('products',$products)->with('suppliers',$suppliers)->with('categories',$categories);
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- $this->validate(request(), [
- 'suppliers' => 'required',
- 'category' => 'required',
- 'model' => 'nullable|string',
- 'product' => 'required|string',
- 'quantity' => 'required|numeric',
- 'price' => 'required'
- ]);
- $products = Products::find($id);
- $products->suppliers_id = $request->input('suppliers');
- $products->category_id = $request->input('category');
- $products->model = $request->input('model');
- $products->products_name = $request->input('product');
- $products->quantity = $request->input('quantity');
- $products->unit_price = str_replace(',',"", $request->input('price'));
- $products->user_id = auth()->user()->id;
- $products->save();
- return redirect('products/index')->with('success', 'Successfully updated product');
- }
- public function search(Request $request)
- {
- $search = $request->input('search');
- $productsearch = Products::orderBy('created_at','desc')->where('products_name', 'like', '%'. $search .'%')
- ->orWhere('unit_price', 'like', '%'. $search .'%')->orWhere('model', 'like', '%'. $search .'%')
- ->orWhereHas('suppliers', function($q) use($search) {
- $q->where('company_name', 'like', '%' . $search . '%');
- })->orWhereHas('category', function($q) use($search) {
- $q->where('category', 'like', '%' . $search . '%');
- })->paginate(5);
- $i = 0;
- foreach($productsearch as &$products){
- $product[$i] = str_replace($search,"<span class='highlight'>$search</span>",$products);
- $i++;
- }
- // dd($products);
- return view('products.index')->with('products',$product);
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- $products = Products::find($id);
- $products->delete();
- return redirect('products/index')->with('products', $products)->with('success', 'Successfully deleted product');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement