检查Laravel 5.3的HTTP请求中是否存在$ _GET var

时间:2016-10-28 12:02:18

标签: php request laravel-5.3

Laravel在其Request类中有一个方法,用于检查请求中是否存在变量。

$request->has('varnamegoeshere')

我需要执行反向操作并检查请求中是否存在变量。当然,您会认为以下方法可行,但事实并非如此:

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

如何检查请求中是否存在变量?

更新:以下控制器和类代码

控制器

<?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);
        }
    }
}

雄辩的课程

<?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;
    }
}

查看档案

@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

2 个答案:

答案 0 :(得分:0)

使用exists代替has

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

希望这有帮助!

答案 1 :(得分:0)

我发现了我的问题。我的视图文件中的if语句具有单数&#34; make&#34;而不是复数。

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

应该是

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