Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在重定向时显示laravel错误消息_Php_Laravel 5 - Fatal编程技术网

Php 在重定向时显示laravel错误消息

Php 在重定向时显示laravel错误消息,php,laravel-5,Php,Laravel 5,我有一个表单,我希望用它向mysql插入一些数据。我已经像这样设置了验证器 public function insert_post(){ $rules = array( 'model' => 'required', 'country' => 'required', 'engine' => 'required' ); $validator =

我有一个表单,我希望用它向mysql插入一些数据。我已经像这样设置了验证器

public function insert_post(){
        $rules = array(
            'model' => 'required',
            'country' => 'required',
            'engine' => 'required'
                 );

        $validator = Validator::make(Input::all(), $rules);
        if ($validator->fails()) {

            // get the error messages from the validator
            $messages = $validator->messages();
            echo '<pre>';
            print_r($messages);
            echo '</pre>';
            return Redirect::to('create-posts')
                ->withErrors($validator);
        }
        else {
            $i = new Text;
            $i->model = request('model');
            $i->country = request('country');
            $i->engine = request('engine');
            $i->save();
            return redirect('/');
        }
    }
但是,它没有显示错误,因为我认为我正在加载一个新的
create posts
,并且验证程序消息丢失

视图代码


模型
@if($errors->has('model'))

{{{$errors->first('model')}

@endif

这就是原因吗?

如果要返回到上一个视图,可以使用:

return Redirect::back()->withErrors($validator)


而不是
return Redirect::to('create-posts')->withErrors($validator)

laravel 5.4版是否正在访问视图中的
$errors
变量?您可以发布视图代码吗?更愿意使用
return Redirect::back()->withErrors($validator)
,尝试在视图上打印
$errors
变量,查看是否未设置:)@Troyer
return Redirect::back()->withErrors($validator)工作。
 public function create_post(){
        return view('create-posts');
    }
 <div class="form-group">
     <label for="inputEmail" class="control-label col-xs-2">Model</label>
        <div class="col-xs-10">
           <input type="text" class="form-control" id="inputEmail" name="model" placeholder="Model">
           @if ($errors->has('model')) <p class="help-block">{{ $errors->first('model') }}</p> @endif
           </div>
        </div>