app/Http/Controllers/Dashboard/ProductController.php
/**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
Display a listing of the resource.
@return \Illuminate\Http\Response
Display a listing of the resource . @return \ Illuminate \ Http \ Response
public function index(Request $request)
    {
          $sort_query = [];
          $sorted = "";
          if ($request->sort !== null) {
              $slices = explode(' ', $request->sort);
              $sort_query[$slices[0]] = $slices[1];
              $sorted = $request->sort;
          }
          if ($request->keyword !== null) {
              $keyword = rtrim($request->keyword);
              $total_count = Product::where('name', 'like', "%{$keyword}%")->orwhere('id', "{$keyword}")->count();
              $products = Product::where('name', 'like', "%{$keyword}%")->orwhere('id', "{$keyword}")->sortable($sort_query)->paginate(15);
          } else {
              $keyword = "";
              $total_count = Product::count();
              $products = Product::paginate(15);
          }
          $sort = [
              '価格の安い順' => 'price asc',
              '価格の高い順' => 'price desc',
              '出品の古い順' => 'updated_at asc',
              '出品の新しい順' => 'updated_at desc'
          ];
          return view('dashboard.products.index', compact('products', 'sort', 'sorted', 'total_count', 'keyword'));
    }
public function index ( Request $ request ) { $ sort_query = [ ] ; $ sorted = " " ; if ( $ request -> sort !== null ) { $ slices = explode ( ' ' , $ request -> sort ) ; $ sort_query [ $ slices [ 0 ] ] = $ slices [ 1 ] ; $ sorted = $ request -> sort ; } if ( $ request -> keyword !== null ) { $ keyword = rtrim ( $ request -> keyword ) ; $ total_count = Product :: where ( 'name' , 'like' , " % { $ keyword } % " ) -> orwhere ( 'id' , " { $ keyword } " ) -> count ( ) ; $ products = Product :: where ( 'name' , 'like' , " % { $ keyword } % " ) -> orwhere ( 'id' , " { $ keyword } " ) -> sortable ( $ sort_query ) -> paginate ( 15 ) ; } else { $ keyword = " " ; $ total_count = Product :: count ( ) ; $ products = Product :: paginate ( 15 ) ; } $ sort = [ '価格の安い順' => 'price a c' '価格の高い順' => 'price d sc , '出品の古い順' => 'updated at asc', '出品の新しい順' => 'updated_a  d sc' ] ; return view ( 'dashboard.products.index' , compact ( 'products' , 'sort' , 'sorted' , 'total_count' , 'keyword' ) ) ; }
Display a listing of the resource.
Display a listing of the resource .