laravel 未定义的偏移量:0

5 laravel

我正在尝试显示错误消息,以防所选字段在数据库中重复。为此,我使用 laravel 验证需要唯一。我在重定向时遇到问题 这是商店控制器

 public function store() {
        $rules = array(
            'car' => array('required', 'unique:insur_docs,car_id'),
        );

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

        if ($validation->fails()) {
            // Validation has failed.
            return Redirect::to('insur_docs/create')->with_input()->with_errors($validation);
        } else {
            $data = new InsurDoc();
            $data->ownership_cert = Input::get('ownership_cert');
            $data->authoriz = Input::get('authoriz');
            $data->drive_permis = Input::get('drive_permis');
            $data->sgs = Input::get('sgs');
            $data->tpl = Input::get('tpl');
            $data->kasko = Input::get('kasko');
            $data->inter_permis = Input::get('inter_permis');
            $data->car_id = Input::get('car');
            $data->save();
            // redirect
            return Redirect::to('/');
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是路线

Route::get('insur_docs/create', array('as' => 'insur_docs.create','uses' => 'Insur_DocController@create'));
Run Code Online (Sandbox Code Playgroud)

创建控制器

public function create() {


     $cars = DB::table('cars')->orderBy('Description', 'asc')->distinct()->lists('Description', 'id');
        return View::make('pages.insur_docs_create', array(
                    'cars' => $cars
        ));

}
Run Code Online (Sandbox Code Playgroud)

insur_docs_create.blade.php

<div id="div-1" class="body">
            {{ Form::open(array('url' => 'insur_docs/store', 'class'=>'form-horizontal','id'=>'inline-validate')) }} 
            <div class="form-group">
                {{ Form::label('ownership_cert', 'Ownership Certificate', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Form::select('ownership_cert', array('' => '', '1' => 'Yes', '0' => 'No'), '',  array(
                                'data-validation' => 'required', 
                                'data-validation-error-msg' => 'You did not enter a valid ownership certificate',
                                'class' => 'form-control'))
                    }}
                </div>
            </div>
            <div class="form-group">
                {{ Form::label('authoriz', 'Authorization', array('class'=>'control-label col-lg-4')) }}            
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('authoriz', '' , array(
                                'data-validation' => 'required', 
                                'data-validation-error-msg' => 'You did not enter a valid authorization date',
                                'class' => 'form-control')) 
                    }}
                </div>
            </div>
            <div class="form-group">
                {{ Form::label('drive_permis', 'Drive Permission', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Form::select('drive_permis', array('' => '', '1' => 'Active', '0' => 'Not active'),  '',  array(
                                'data-validation' => 'required', 
                                'data-validation-error-msg' => 'You did not enter a valid drive permission',
                                'class' => 'form-control'))
                    }}
                </div>
            </div>
            <div class="form-group">
                {{ Form::label('sgs', 'SGS', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('sgs', '' , array(
                                'data-validation' => 'required', 
                                'data-validation-error-msg' => 'You did not enter a valid sgs date',
                                'class' => 'form-control')) 
                    }}
                </div>
            </div>  
            <div class="form-group">
                {{ Form::label('tpl', 'TPL', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('tpl', isset($v->sgs) ? $v->sgs : '' , array(
                                'data-validation' => 'required', 
                                'data-validation-error-msg' => 'You did not enter a valid tpl date',
                                'class' => 'form-control')) 
                    }}
                </div>
            </div>
            <div class="form-group">
                {{ Form::label('kasko', 'Kasko', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('kasko', isset($v->kasko) ? $v->kasko : '' , array(
                                'data-validation' => 'required', 
                                'data-validation-error-msg' => 'You did not enter a valid kasko date',
                                'class' => 'form-control')) 
                    }}
                </div>
            </div>
            <div class="form-group">
                {{ Form::label('inter_permis', 'International Permission', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Helpers\Helper::date('inter_permis', '' , array(
                                'data-validation' => 'required', 
                                'data-validation-error-msg' => 'You did not enter a valid international permission date',
                                'class' => 'form-control')) 
                    }}
                </div>
            </div>
            <div class="form-group">
                {{ Form::label('car', 'Car', array('class'=>'control-label col-lg-4')) }}
                <div class="col-lg-8">
                    {{ Form::select('car', $cars, Input::old('class'), array(
                                'data-validation' => 'required',  
                                'data-validation-error-msg' => 'You did not enter a valid car',
                                'class' => 'form-control')) 
                    }}
                    {{ $errors->first('car') }}
                </div>
            </div>
            {{ Form::submit('Save', array('class' => 'btn btn-success btn-line')) }}
            <input type="button" value="Back" class="btn btn-danger btn-line" onClick="history.go(-1);
                    return true;">
            <div>
                @foreach($errors as $error)
                <li>{{$error}}</li>
                @endforeach
            </div>
            {{ Form::close() }}
Run Code Online (Sandbox Code Playgroud)

它显示此错误:

未定义的偏移量:0

sid*_*ber 4

您可能正在使用 a get,使用post可能会有所帮助。除此之外,您正在混合modelcontroller编码。将它们分开总是一个好主意。例如,您的重定向应该在控制器内部完成,而不是在模型中完成。

最好还是继续执行一些操作$validator->passes(),然后else返回错误。

控制器

public function store() {

    $data = [
       "errors" => null
    ];

    $rules = array(
        'car' => array('required', 'unique:insur_docs,car_id')
    );

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

    if($validation->passes()) {
        $data = new InsurDoc();
        $data->ownership_cert = Input::get('ownership_cert');
        $data->authoriz = Input::get('authoriz');
        $data->drive_permis = Input::get('drive_permis');
        $data->sgs = Input::get('sgs');
        $data->tpl = Input::get('tpl');
        $data->kasko = Input::get('kasko');
        $data->inter_permis = Input::get('inter_permis');
        $data->car_id = Input::get('car');
        $data->save();
        return Redirect::to('/');
    } else {
        $data['errors'] = $validation->errors();
        return View::make('pages.insur_docs_create', $data);
    }

}
Run Code Online (Sandbox Code Playgroud)

您的错误将显示在您的视图中$errors{{var_dump($errors)}}只需在模板中执行 ablade即可验证它们是否存在。

看法

@if($errors->count() > 0)
  <p>The following errors have occurred:</p>

  <ul>
    @foreach($errors->all() as $message)
      <li>{{$message}}</li>
    @endforeach
  </ul>
@endif
Run Code Online (Sandbox Code Playgroud)