laravel 未定义偏移量:0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23538053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel Undefined offset: 0
提问by
I am trying to diplay an error mesasge in case the field selected is duplicated in db.For this I am using laravel validation required unique. I am having problem with redirect Here is store controller
我正在尝试显示错误消息,以防所选字段在 db 中重复。为此,我使用 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('/');
}
}
Here is the route
这是路线
Route::get('insur_docs/create', array('as' => 'insur_docs.create','uses' => 'Insur_DocController@create'));
create controller
创建控制器
public function create() {
$cars = DB::table('cars')->orderBy('Description', 'asc')->distinct()->lists('Description', 'id');
return View::make('pages.insur_docs_create', array(
'cars' => $cars
));
}
insur_docs_create.blade.php
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() }}
I t displays this error :
它显示此错误:
Undefined offset: 0
未定义的偏移量:0
采纳答案by sidneydobber
It might be that you are using a get
, using post
might help. Other than that you are mixing model
and controller
code. It's always a good idea to seperate these. For instance your redirects should be done inside the controller and not in the model.
可能是您正在使用get
,使用post
可能会有所帮助。除此之外,您正在混合model
和controller
编码。将这些分开总是一个好主意。例如,您的重定向应该在控制器内部完成,而不是在模型中完成。
- http://laravel.com/docs/validation
- http://laravelbook.com/laravel-input-validation/
- http://culttt.com/2013/07/29/creating-laravel-4-validation-services/
- http://laravel.com/docs/validation
- http://laravelbook.com/laravel-input-validation/
- http://culttt.com/2013/07/29/creating-laravel-4-validation-services/
It's also better to do stuff on $validator->passes()
and then else
return with errors.
最好先做一些事情$validator->passes()
然后else
返回错误。
Controller
控制器
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);
}
}
Your errors will be available in your view under $errors
. Just do a {{var_dump($errors)}}
in your blade
template to verify that they are there.
您的错误将显示在您的视图中$errors
。只需{{var_dump($errors)}}
在您的blade
模板中执行 a以验证它们是否存在。
View
看法
@if($errors->count() > 0)
<p>The following errors have occurred:</p>
<ul>
@foreach($errors->all() as $message)
<li>{{$message}}</li>
@endforeach
</ul>
@endif
回答by Bakly
I Think this is a really better answer from this refrenece Laravel 4, ->withInput(); = Undefined offset: 0
我认为这是来自参考Laravel 4, ->withInput(); 的一个更好的答案 。= 未定义的偏移量:0
withInput()
doesn't work the way you think it does. It's only a function of Redirect, not View.
withInput()
不像你认为的那样工作。它只是重定向的功能,而不是视图。
Calling withInput($data)
on View has a completely different effect; it passes the following key value pair to your view: 'input' => $data
(you get an error because you're not passing any data to the function)
调用withInput($data)
View有完全不同的效果;它将以下键值对传递给您的view: 'input' => $data
(您收到错误,因为您没有将任何数据传递给函数)
To get the effect that you want, call Input::flash()
before making your view, instead of calling withInput()
. This should allow you to use the Input::old()
function in your view to access the data.
要获得您想要的效果,请Input::flash()
在创建视图之前调用,而不是调用withInput()
. 这应该允许您Input::old()
在视图中使用该函数来访问数据。
Alternatively, you could simply pass Input::all()
to your view, and use the input[]
array in your view:
或者,您可以简单地传递Input::all()
给您的视图,并input[]
在您的视图中使用该数组:
View::make(...)->withInput(Input::all());
which is translated to
这被翻译成
View::make(...)->with('input', Input::all());
As for your comment, I recommend doing it like so:
至于你的评论,我建议这样做:
$position_options = DB::table('jposition')->lists('friendly','id');
$category_options = DB::table('jcategory')->lists('friendly','id');
$location_options = DB::table('jlocation')->lists('friendly','id');
$category = Input::get('category');
$location = Input::get('location');
$type = Input:: get('type');
$data = compact('position_options', 'category_options', 'location_options', 'category', 'type', 'location');
return View::make('jobsearch.search', $data);
回答by Hasith Lakshan
also thinks about laravel resource controller. because when we call no parameter get method, it redirects to the show method with ignoring our function name.
还考虑了 Laravel 资源控制器。因为当我们调用没有参数的 get 方法时,它会忽略我们的函数名称而重定向到 show 方法。
eg:-Route::get('hasith', 'Order\OrderController@hasith');-----> this parameter rederect to this function
eg:-Route::get('hasith', 'Order\OrderController@hasith');----->这个参数重新指向这个函数
public function show($id, Request $request) {
//code
}