无法在Laravel 5上显示错误消息

时间:2016-01-20 10:42:18

标签: php laravel laravel-5

我在使用$ this-> validate($ request,[]);

时遇到显示错误消息的问题

路线发布并获取

Route::post('{phone}/{name}/store_location','RestaurantController@store_location'); 
Route::get('{phone}/{name}','RestaurantController@show');

Store_location控制器

public function store_location($phone, $name ,Restaurant_LocationRequest $request)
{   
    $this->validate($request, [
        'street'      => 'required',
        'city'        => 'required',
        'zip'         => 'required',
        'country'     => 'required',
        'state'       => 'required'
    ]);

    $restaurant_location = Restaurant::locatedAt($phone, $name)->first();

    $restaurant_location->restaurant_location()->create($request->all());

    return redirect($restaurant_location->phone . '/' . $restaurant_location->name);
}

表单显示错误

<form role="form" method="POST" action="/{{ $restaurant->phone }}/{{ $restaurant->name }}/store_location">

    @include('restaurant.create_restaurant_location_form')

    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

</form>

在下面是我的请求文件 Restaurant_LocationRequest Reques

public function authorize()
{
        return true;
}

public function rules()
{
    return [
        'street'      => 'required',
        'city'        => 'required',
        'zip'         => 'required',
        'country'     => 'required',
        'state'       => 'required'
    ];
}

1 个答案:

答案 0 :(得分:0)

尝试,

$myErrors = $this->validate($request, [
        'street'      => 'required',
        'city'        => 'required',
        'zip'         => 'required',
        'country'     => 'required',
        'state'       => 'required'
    ]);

.............
...............

if($myErrors->fails()){
    return redirect($restaurant_location->phone . '/' . $restaurant_location->name)->withErrors($myErrors->errors());
}