Search code examples
phprequestlaravel-5.3

Checking that a $_GET var doesn't exist in Laravel 5.3's HTTP Request


Laravel has a method in its Request class that checks if a variable exists in the request.

$request->has('varnamegoeshere')

I need to do the inverse and check if a variable doesn't exist in the request. Naturally you would think the following would work, but it doesn't:

if(!$request->has('varnamegoeshere'))

How do I go about checking if a variable doesn't exist in the request?

UPDATE: Controller and class code below

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Project\Frontend\Repo\Vehicle\VehicleInterface;
use Illuminate\Support\Facades\View;

class SearchController extends BaseController
{
    protected $vehicle;

    public function __construct(
        VehicleInterface $vehicle
    )
    {
        $this->vehicle  = $vehicle;
    }

    public function index(Request $request)
    {
        /*
         * Search Refinement
         */

        // Set links that will refine user selection when clicked
        $search = array();

        if(!$request->has('make'))
        {
            // Fetch makes for user refinement
            $search['makes'] = $this->vehicle->setMakeLinks($request);
        }

        if(View::exists('index'))
        {
            return View::make('search')
                ->with('search', $search);
        }
    }
}

Eloquent class

<?php

namespace App\Project\Frontend\Repo\Vehicle;

use Illuminate\Database\Eloquent\Model;

class EloquentVehicle implements VehicleInterface
{
    /**
     * Protected variables
     *
     * @var Model
     */
    protected $vehicle;

    /**
     * EloquentVehicle constructor.
     *
     * @param Model $vehicle
     */
    public function __construct(
        Model $vehicle
    )
    {
        $this->vehicle = $vehicle;
    }

    /**
     * Create links based on unique makes retrieved from the database
     *
     * @param null $request
     * @return mixed
     */
    public function setMakeLinks($request = null)
    {
        $query = $this->vehicle->groupBy('make')
            ->orderBy('make', 'asc')
            ->get();

        // Create empty array
        $links = array();

        // Obtain query string
        $queryString = str_replace($request->url(), '', $request->fullUrl());

        foreach($query as $vehicle)
        {
            $append = '?make='.$vehicle->make;
            if ($queryString != '')
                $append = $queryString.'&make='.$vehicle->make;

            array_push($links, '<a title="'.$vehicle->make.' for sale" itemprop="brand" href="'.url('usedcars/'.$append.'#searched').'">'.$vehicle->make.'</a>');
        }

        return $links;
    }
}

View file

@extends('layouts.template')

@section('content')
    @if(isset($search['make']))
    <div id="refine" class="makes" itemscope itemtype="http://schema.org/AutomotiveBusiness">
        <h2>Find cars by Make</h2>
        <div id="choices">
            @foreach($search['make'] as $link)
                {!! $link !!}
            @endforeach
            <div id="clear-both"></div>
        </div>
    </div>
    @endif
@endsection

Solution

  • I found my problem. The if statement in my view file had the singular "make" instead of the plural.

    @if(isset($search['make']))
    

    should be

    @if(isset($search['makes']))