缺少 UserController::edit() 的参数 2 - Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20731473/
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
Missing argument 2 for UserController::edit() - Laravel
提问by Gilko
I'm using the framework Laravel.
我正在使用框架 Laravel。
I have 2 tables (Users and Persons). When I want to edit the user and at the same time the person, I get the error message:
我有 2 个表(用户和人员)。当我想同时编辑用户和人时,我收到错误消息:
Missing argument 2 for UserController::edit()
UserController::edit() 缺少参数 2
Table Users
表用户
CREATE TABLE IF NOT EXISTS `festival_aid`.`users` (
`user_id` BIGINT NOT NULL AUTO_INCREMENT,
`user_username` VARCHAR(45) NOT NULL,
`user_email` VARCHAR(45) NOT NULL,
`user_password` CHAR(32) NOT NULL,
`user_salt` CHAR(32) NOT NULL,
`user_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_modified` TIMESTAMP NULL,
`user_deleted` TIMESTAMP NULL,
`user_lastlogin` TIMESTAMP NULL,
`user_locked` TIMESTAMP NULL,
`user_token` VARCHAR(128) NULL,
`user_confirmed` TIMESTAMP NULL,
PRIMARY KEY (`user_id`, `person_id`),
UNIQUE INDEX `user_email_UNIQUE` (`user_email` ASC),
INDEX `fk_users_persons1_idx` (`person_id` ASC),
CONSTRAINT `fk_users_persons1`
FOREIGN KEY (`person_id`)
REFERENCES `festival_aid`.`persons` (`person_id`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Table Persons
表人
CREATE TABLE IF NOT EXISTS `festival_aid`.`persons` (
`person_id` BIGINT NOT NULL AUTO_INCREMENT,
`person_firstname` VARCHAR(45) NULL,
`person_surname` VARCHAR(45) NULL,
`person_created` TIMESTAMP NOT NULL,
`person_modified` TIMESTAMP NULL,
`person_deleted` TIMESTAMP NULL,
PRIMARY KEY (`person_id`))
ENGINE = InnoDB;
Model Person
模范人物
class Person extends Eloquent {
protected $table = 'persons';
protected $primaryKey = 'person_id';
public function person()
{
return $this->belongsTo('User');
}
public $timestamps = false;
}
The UserController with edit and update
具有编辑和更新功能的 UserController
public function edit($user_id, $person_id)
{
$user = User::find($user_id);
$person = Person::find($person_id);
return View::make('users.edit')
->with('user', $user)
->with('person', $person);
}
public function update($user_id, $person_id)
{
$rules = array(
'person_firstname' => 'required',
'person_lastname' => 'required',
'user_username' => 'required|unique:users',
'user_email' => 'required|unique:users|email',
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
Redirect::to('users/' . $user_id . '/' . $person_id . '/edit')
->withErrors($validator)
->withInput(Input::except('user_password'));
} else {
$user = User::find($user_id);
$person = Person::find($person_id);
$person->person_firstname = Input::get('person_firstname');
$person->person_lastname = Input::get('person_lastname');
$user->user_username = Input::get('user_username');
$user->user_email = Input::get('user_email');
$user->save();
$person->save();
Session::flash('message', 'Successfully updated user!');
return Redirect::to('users/index');
}
View Edit
查看编辑
<legend>Edit {{ $person->person_firstname }}</legend>
{{ HTML::ul($errors->all()) }}
{{ Form::model($user, $person, array('method' => 'PUT')) }}
<div class="form-group">
{{ Form::label('firstname', 'Firstname') }}
{{ Form::text('person_firstname', Input::old('firstname'), array('class'=>'form-control', 'placeholder' => 'Firstname')) }}
</div>
<div class="form-group">
{{ Form::label('surname', 'Surname') }}
{{ Form::text('person_surname', Input::old('surname'), array('class'=>'form-control', 'placeholder' => 'Surname')) }}
</div>
<div class="form-group">
{{ Form::label('username', 'Username') }}
{{ Form::text('user_username', Input::old('username'), array('class'=>'form-control', 'placeholder' => 'Username')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::text('user_email', Input::old('email'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('Password') }}
{{ Form::password('user_password', array('class'=>'form-control', 'placeholder' => 'Password')) }}
</div>
{{ Form::submit('Edit the User!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
The Route
路线
Route::get('/users/{user_id}/edit', 'UserController@edit');
What am I doing wrong?
我究竟做错了什么?
回答by SamV
Your function declaration specifies two parameters:
您的函数声明指定了两个参数:
public function edit($user_id, $person_id)
Yet the route only injects one parameter user_id
.
然而路由只注入一个参数user_id
。
Route::get('/users/{user_id}/edit', 'UserController@edit');
It should look like this:
它应该是这样的:
Route::get('/users/{user_id}/{person_id}/edit', 'UserController@edit');
Or remove $person_id
from the function declaration.
或者$person_id
从函数声明中删除。