htmlspecialchars()期望参数1为字符串,给定laravel的数组

时间:2017-03-17 07:29:01

标签: php arrays laravel laravel-5.4

我正在尝试根据搜索页面中的价格(从高到低和从低到高)缩短记录(游览),并在变更事件中提交表单。以下是表单的代码:

{!!Form::open(['route' => 'short','method' => 'GET', 'role' => 'search','class'=>'select-filters'])!!}
    <input type="hidden" name="country" value="{{$country}}">
    <input type="hidden" name="category" value="{{$category}}">
    <input type="hidden" name="days" value="{{$days}}">
    <select name="sort_price" id="sort-price"  onchange="this.form.submit()">
    <option value="" selected="">Sort by price</option>
    <option value="lower">Lowest price</option>
    <option value="higher">Highest price</option>
    </select>
{!! Form::close() !!}

我已在同一页面初始化了这些值,但却出现错误htmlspecialchars() expects parameter 1 to be string, array given

@if(Request::only('country') != null)
    {{$country = Request::only('country')}}
@else
    {{ $country = null }}
@endif
@if(Request::only('category') != null)
    {{$category = Request::only('category')}}
@else
    {{ $category = null }}
@endif
@if(Request::only('days') != null)
    {{$days = Request::only('days')}}
@else
    {{ $days = null }}
@endif

Request::only()中的值正在索引页面的搜索表单中传递。当我死并倾倒时:

{{dd(Request::only('country','category','days'))}}

我以数组形式

抛出传入的值

enter image description here 如果我只传递一个参数,它会转储单个键值对:

    {{dd(Request::only('country'))}}

        {{dd(Request::only('category'))}}

        {{dd(Request::only('days'))}}

1 个答案:

答案 0 :(得分:1)

only()总是返回一个数组,所以要获得一个字符串,请执行以下操作:

{{ request('country') }}

而不是:

{{ Request::only('country') }}