app/Http/Controllers/HomeController.php
/**
     * Show the homepage / ad overview
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
Show the homepage / ad overview
@return \Illuminate\Contracts\Support\Renderable
Show the homepage / ad overview @return \ Illuminate \ Contracts \ Support \ Renderable
public function index(Request $request)
    {
        $filterBrandstof = $request->get('brandstof');
        $filterMerk = $request->get('merk');
        $brands = [];
        if ($filterBrandstof || $filterMerk) {
            $query = Ad::query();
            if ($filterBrandstof) {
                $query->where('fuel_type_id', '=', $filterBrandstof);
            }
            if ($filterMerk) {
                if (strpos($filterMerk, ',') !== false) {
                    $brands = explode(",", $filterMerk);
                } else {
                    $brands[] = $filterMerk;
                }
                $query->where( function ( $query ) use ($brands)
                {
                    $i = 0;
                    foreach ($brands as $brand) {
                        $i++;
                        if ($i == 1) {
                            $query->where('brand', '=', $brand);
                        } else {
                            $query->orWhere('brand', '=', $brand);
                        }
                    }
                });
            }
            $ads = $query->get();
        } else {
            $ads = Ad::all();
        }
        $fuelTypes = FuelType::all();
        $brandOptions = [];
        $allAds = Ad::all();
        foreach ($allAds as $ad) {
            if (!in_array($ad->brand, $brandOptions)) {
                $brandOptions[] = $ad->brand;
            }
        }
        return view('home', compact('ads', 'brandOptions', 'fuelTypes', 'filterBrandstof', 'brands'));
    }
public function index ( Request $ request ) { $ filterBrandstof = $ request -> get ( 'brandstof' ) ; $ filterMerk = $ request -> get ( 'merk' ) ; $ brands = [ ] ; if ( $ filterBrandstof || $ filterMerk ) { $ query = Ad :: query ( ) ; if ( $ filterBrandstof ) { $ query -> where ( 'fuel_type_id' , '=' , $ filterBrandstof ) ; } if ( $ filterMerk ) { if ( strpos ( $ filterMerk , ',' ) !== false ) { $ brands = explode ( " , " , $ filterMerk ) ; } else { $ brands [ ] = $ filterMerk ; } $ query -> where ( function ( $ query ) use ( $ brands ) { $ i = 0 ; foreach ( $ brands as $ brand ) { $ i ++ ; if ( $ i == 1 ) { $ query -> where ( 'brand' , '=' , $ brand ) ; } else { $ query -> orWhere ( 'brand' , '=' , $ brand ) ; } } } ) ; } $ ads = $ query -> get ( ) ; } else { $ ads = Ad :: all ( ) ; } $ fuelTypes = FuelType :: all ( ) ; $ brandOptions = [ ] ; $ allAds = Ad :: all ( ) ; foreach ( $ allAds as $ ad ) { if ( ! in_array ( $ ad -> brand , $ brandOptions ) ) { $ brandOptions [ ] = $ ad -> brand ; } } return view ( 'home' , compact ( 'ads' , 'brandOptions' , 'fuelTypes' , 'filterBrandstof' , 'brands' ) ) ; }
Show the homepage / ad overview
 @return \Illuminate\Contracts\Support\Renderable
Show the homepage / ad overview @return \ Illuminate \ Contracts \ Support \ Renderable
//if any filters are activated, perform these filter queries