app/Http/Controllers/ProductController.php
/**
     * Display a listing of the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Display a listing of the resource.
Display a listing of the resource .
public function index(Request $request)
    {
        $products = $this->product->with(['category.sizes', 'images']);
        if(!empty($request->category) && $request->category != 'all') {
            $products = $products->whereHas('category', function ($query) use ($request) {
                $query->where('name', $request->category);
            });
        }
        if(!empty($request->search)) {
            $products = $products->where('name', 'like', "%{$request->search}%");
        }
        if(!empty($request->sort) && in_array(strtolower($request->sort), ['asc', 'desc'])) {
            $products = $products->orderBy('price', strtolower($request->sort));
        } else {
            $products = $products->orderBy('price', 'asc');
        }
        $products = $products->paginate(9);
        foreach($products as $product) {
            $product->before_discount = $product->price * 100/78;
        }
        $products->appends(request()->input())->links();
        $categories = $this->category->groupBy('name')->orderBy('name')->get(['name']);
        return view('web.product.index', compact('categories', 'products'));
    }
public function index ( Request $ request ) { $ products = $ this -> product -> with ( [ 'category.sizes' , 'images' ] ) ; if ( ! empty ( $ request -> category ) && $ request -> category != 'all' ) { $ products = $ products -> whereHas ( 'category' , function ( $ query ) use ( $ request ) { $ query -> where ( 'name' , $ request -> category ) ; } ) ; } if ( ! empty ( $ request -> search ) ) { $ products = $ products -> where ( 'name' , 'like' , " % { $ request -> search } % " ) ; } if ( ! empty ( $ request -> sort ) && in_array ( strtolower ( $ request -> sort ) , [ 'asc' , 'desc' ] ) ) { $ products = $ products -> orderBy ( 'price' , strtolower ( $ request -> sort ) ) ; } else { $ products = $ products -> orderBy ( 'price' , 'asc' ) ; } $ products = $ products -> paginate ( 9 ) ; foreach ( $ products as $ product ) { $ product -> before_discount = $ product -> price * 100 / 78 ; } $ products -> appends ( request ( ) -> input ( ) ) -> links ( ) ; $ categories = $ this -> category -> groupBy ( 'name' ) -> orderBy ( 'name' ) -> get ( [ 'name' ] ) ; return view ( 'web.product.index' , compact ( 'categories' , 'products' ) ) ; }
Display a listing of the resource.
Display a listing of the resource .