Laravel Isset错误显示[]

时间:2014-07-21 16:04:57

标签: laravel laravel-4 isset

为什么每次我加载页面时都没有正确加载它仍然向页面显示这样的数组 []?

浏览

@if(isset($errors))
{{$errors}}
@endif

控制器

 $data = Input::all();

if($errors = $this->deliveryReport->isInvalid($data))
{
    return Redirect::back()->withInput()->withErrors($errors);
}

2 个答案:

答案 0 :(得分:3)

这是收藏演示的最常见用途:

@if($collection->isEmpty())
    <h2>No items were found</h2>
@else
    <h2>The following {{$collection->count()}} items were found</h2>
    @foreach($collection as $c)
        {{ $c->someAttribute }}
    @endforeach
@endif

现在特别针对$errors

@if($errors->any())
    <div id="error-box">
        @foreach ($errors->all() as $error)
            <div>{{ $error }}</div>
        @endforeach
    </div>
@endif

请确保在验证完成后正确地将$errors传递给您的视图。所以在你的控制器中:

$rules = [...];

$v = Validator::make(Input::all(), $rules);

if($v->fails())
{
    return Redirect::back()->withInput()->withErrors($v);
}

...

答案 1 :(得分:1)

withError()使它成为MessageBag的实例,你可以在其上使用方法:

@foreach($errors->all() as $error)
    {{ $error }}
@endforeach

或查看docs