我想我的路线有错误,但我开始了,我找不到解决方案。
当我提交编辑表单时,出现此错误:Missing argument 2 for App\Http\Controllers\Admin\SportController::update()
我的控制器:
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('admin');
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
// Récupère tous les sports
$sports = Sport::all();
// Charge la vue et passe la liste des sports en paramètre
return view('admin.sports.index')
->with('sports', $sports);
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
return view('admin.sports.create');
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|max:255',
]);
// process the login
if ($validator->fails()) {
return Redirect::back()->withErrors($validator)
->withInput();
}
Sport::create($request->all());
Session::flash('message', 'Félicitation, vous avez crée un sport !');
return redirect('/admin/sports');
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
// get the nerd
$sport = Sport::find($id);
// show the edit form and pass the nerd
return view('admin.sports.edit')->with('sport', $sport); }
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), [
'name' => 'required|max:255',
]);
// process the login
if ($validator->fails()) {
return Redirect::back()->withErrors($validator)
->withInput();
}
$sport = Sport::find($id);
$sport->name= Input::get('name');
$sport->save();
Session::flash('message', 'Félicitation, vous avez mis à jour un sport !');
return redirect('/admin/sports');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
我的路线:
Route::resource('/admin/sports', 'Admin\SportController@index');
Route::resource('/admin/sports/create', 'Admin\SportController@create');
Route::POST('/admin/sports', 'Admin\SportController@store');
Route::get('/admin/sports/edit/{id}','Admin\SportController@edit');
Route::put('/admin/sports','Admin\SportController@update')->name('admin.sports.update');
我的观点:edit.blade.php
{{ Html::ul($errors->all()) }}
{{ Form::model($sport, array('route' => array('admin.sports.update', $sport->id), 'method' => 'PUT')) }}
<div class="form-group">
{{ Form::label('name', 'Nom') }}
{{ Form::text('name', null, array('class' => 'form-control')) }}
</div>
<div class="right-align">
{{ Form::submit('Valider', array('class' => 'btn btn-flat')) }}
<a class="btn btn-flat" href="{{ URL::to('admin/sports') }}">Annuler</a>
</div>
{{ Form::close() }}
感谢您的帮助。 彼得。
答案 0 :(得分:4)
您应该更改此路线以捕获ID:
Route::put('/admin/sports/{id}','Admin\SportController@update')->name('admin.sports.update');
答案 1 :(得分:1)
在您的路线中,您有:
Route::resource('/admin/sports', 'Admin\SportController@index');
对于资源控制器,您不需要指定方法,只需要控制器为您完成剩下的工作。所以当你这样做时:
Route::resource('/admin/sports', 'Admin\SportController');
您将获得以下内容:
Method | Uri | Name | action
GET|HEAD | admin/sports | admin.sports.index | Admin\SportController@index
POST | admin/sports | admin.sports.store | Admin\SportController@store
DELETE | admin/sports/{id} | admin.sports.destroy | Admin\SportController@destroy
PUT|PATCH | admin/sports/{id} | admin.sports.update | Admin\SportController@update
GET|HEAD | admin/sports/{id}/edit | admin.sports.edit | Admin\SportController@edit
看起来你似乎没有将id传递给路线。将您的Form::model
更改为:
Form::model($sport, array('route' => array('admin.sports.update', array('id', $sport->id)), 'method' => 'PUT'))
第二个参数应该是路径参数数组