myszwa

ShowroomPageController

May 23rd, 2022
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.29 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4. use DB;
  5. use Illuminate\Http\Request;
  6. use App\Models\Product;
  7. use App\Models\Category;
  8.  
  9.  
  10. class ShowroomPageController extends Controller
  11. {
  12.     /**
  13.      * Display a listing of the resource.
  14.      *@param string $slug
  15.      * @return \Illuminate\Http\Response
  16.      */
  17.     public function index()
  18.     {
  19.         if (request()->category) {
  20.             $products = Product::with('categories')->whereHas('categories', function ($query) {
  21.                 $query->where('slug', request()->category);
  22.             })->take(0)->paginate(9);
  23.             $products = DB::table('products')
  24.                 ->select('products.*', 'categories.slug as category_slug')
  25.                 ->join('categories', 'categories.id', '=', 'products.category_id')
  26.                 ->where('categories.slug', request()->category)
  27.                 ->orderBy('products.slug')
  28.                 ->take(0)
  29.                 ->paginate(9);
  30.  
  31.             $categories = Category::all();
  32.  
  33.         } else {
  34.             $products = Product::inRandomOrder()->take(0)->paginate(9);
  35.             $categories = Category::all();
  36.         }
  37.  
  38.        
  39.  
  40.         return view('/pages/showroom')->with([
  41.             'products' => $products,
  42.             'categories' => $categories,
  43.         ]);
  44.     }
  45.  
  46.    
  47.  
  48.     /**
  49.      * Display a specified of the resource.
  50.      *
  51.      * @param string $slug
  52.      * @param string $lang
  53.      *
  54.      * @return \Illuminate\Http\Response
  55.      *
  56.      */
  57.     public function show($slug)
  58.     {
  59.         $product = Product::where('products.slug', $slug)
  60.             ->select('products.*', 'categories.slug as category_slug')
  61.             ->addselect('products.*', 'categories.name as category_name')
  62.             ->join('categories', 'categories.id', '=', 'products.category_id')
  63.             ->first();
  64.            
  65.    
  66.         $productsLike = DB::table('products')
  67.             ->select('products.*')
  68.             ->join('categories', 'categories.id', '=', 'products.category_id')
  69.             ->where('categories.slug', $product->category_slug)
  70.             ->inRandomOrder()
  71.             ->take(4)
  72.             ->get();
  73.  
  74.         return view('/pages/product/product')->with([
  75.             'product'=> $product,
  76.             'productsLike' => $productsLike,
  77.         ]);
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment