当我尝试编辑“Metier”时,会创建一个新的“Metier”,而旧的“Metier”保持不变。我想粉碎旧的“Metier”并通过编辑创建一个新的。这是我与编辑功能相关的代码。
控制器
public function edit($id)
{
$metier=Metier::find($id);
return view('metier.edit',['libelle_metier'=>$metier]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$metier=Metier::find($id);
return view('metier.edit',['libelle_metier'=>$metier]);
}
查看
<div class="form-group">
<label for="">libelle Metier </label>
<input type="text" name ="libelle_metier" class="form-control"value ="
{{$libelle_metier->libelle_metier}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control btn btn-
primary">
</div>
路线
Route::get('/metier', 'MetierController@index');
Route::get('/metier/create', 'MetierController@create');
Route::post('/metier', 'MetierController@store');
Route::get('/metier/{id}/show', 'MetierController@edit');
Route::get('/metier/{id}/edit', 'MetierController@edit');
Route::upd('/metier/{id}/update', 'MetierController@update');
Route::delete('/metier/{id}', 'MetierController@destroy')
MetierController.php
public function edit($id)
{
$metier=Metier::find($id);
return view('metier.edit',['libelle_metier'=>$metier]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$metier = Metier::find($id);
$metier->libelle_metier = $request->libelle_metier;
$metier->save();
return back();
}
edit.blade.php
@extends('Layouts/app')
@extends('Layouts.master')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Metier </h1>
<form action=" {{url ('metier') }}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="">libelle Metier </label>
<input type="text" name ="libelle_metier" class="form-
control"value ="
{{$libelle_metier->libelle_metier}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control
btn btn-
primary">
</div>
</form>
</div>
</div>
@endsection
答案 0 :(得分:0)
将{{ method_field('PATCH') }}
添加到您的表单并将操作更改为命名路由并将metier id传递给它。
@extends('Layouts/app')
@extends('Layouts.master')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Metier </h1>
<form action="{{ route('metier.update', $libelle_metier->id) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="">libelle Metier </label>
<input type="text" name ="libelle_metier" class="form-
control"value ="
{{$libelle_metier->libelle_metier}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control
btn btn-
primary">
</div>
</form>
</div>
</div>
@endsection
路线档案
Route::patch('/metier/{id}/update', 'MetierController@update')->name('metier.update');
提示:删除所有这些
Route::get('/metier', 'MetierController@index');
Route::get('/metier/create', 'MetierController@create');
Route::post('/metier', 'MetierController@store');
Route::get('/metier/{id}/show', 'MetierController@edit');
Route::get('/metier/{id}/edit', 'MetierController@edit');
Route::upd('/metier/{id}/update', 'MetierController@update');
Route::delete('/metier/{id}', 'MetierController@destroy')
并将其全部添加为单个资源,以便一次性添加所有REST URL。 如果它的REST规范应该如何。阅读 REST Resource Naming Guide和here
Route::resource('metier', 'MetierController');
或以这种方式添加而不是资源
Route::get('/metier', 'MetierController@index')->name('metier.index');
Route::get('/metier/create', 'MetierController@create')->name('metier.create');
Route::post('/metier', 'MetierController@store')->name('metier.store');
Route::get('/metier/{id}', 'MetierController@show')->name('metier.show');
Route::get('/metier/{id}/edit', 'MetierController@edit')->name('metier.edit');
Route::patch('/metier/{id}', 'MetierController@update')->name('metier.update');
Route::delete('/metier/{id}', 'MetierController@destroy')->name('metier.destroy');
控制器
public function edit($id)
{
$metier=Metier::find($id);
return view('metier.edit',['libelle_metier'=>$metier]);
}
public function update(Request $request, $id)
{
// do some request validation
$metier=Metier::find($id);
$metier->update($request->all());
return redirect()->route('metier.show', $metier->id);
}
如果你有质量分配错误。
将protected $guarded = [];
添加到Metier模型