ProductController.php 1.66 KB
Newer Older
Pancholin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php

namespace Onestartup\ProductResource\Controller;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Yajra\Datatables\Datatables;
use Onestartup\ProductResource\Model\ProductResource as Product;
use Onestartup\ProductResource\Model\ProductResourceCategory as Category;

class ProductController extends Controller
{
    public function index(Request $request)
    {
    	if(isset($request->category)){

            $category = Category::where('slug', $request->category)->first();
19
            $products = $category->products()->where('active', true)->paginate(12);
Pancholin committed
20 21

        } else {
22
            $products = Product::where('active', true)->paginate(12);
Pancholin committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        }
        $otros = Product::where('active', true)->inRandomOrder()->take(3)->get();
        $categories = Category::where('active', true)->get();
        
        //$products;
        return view('product_resource-public::list')
            ->with('categories', $categories)
            ->with('otros', $otros)
            ->with('products', $products);
        
    }

    public function show($slug)
    {

    	$product = Product::where('slug',$slug)->first();
    	$otros = $product->category->products()->where('active', true)->take(3)->get(); 


        $categories = Category::all();

        if ($product != null) {
            if (!$product->active) {
                return redirect('inactivo');
            }
        } else {
            return redirect('no_existe');
        }

        return view('product_resource-public::single')
            ->with('product', $product)
            ->with('categories', $categories)
            ->with('otros', $otros);
        
    }

}