基本控制器中的Destroy()不工作



所以我在表中打印用户投诉,在表中每行都打印一个Delete按钮。当我点击删除按钮时,我想从表中删除该特定投诉。我不是使用资源控制器,而是使用基本控制器。现在,这是我的代码:

ViewComplaintblade.php(带有删除按钮的投诉表(:

<table id="cTable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Student Name</th>
<th>Complaint Title</th>
<th>Complaint Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($complaints as $complaint)
<tr>
<td>{{ $complaint->name }}</td>
<td>{{ $complaint->cname }}</td>
<td>{{ $complaint->cbody }}</td>
<td class="btn-group">
{!! Form::open(array('route'=>['complaint.destroy',$complaint->id],'method'=>'DELETE')) !!}
{!! Form::submit('Delete',['type'=>'submit','style'=>'border-radius: 0px;','class'=>'btn btn-danger btn-sm',$complaint->id]) !!}
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
</table>

Web.php(路由(:

Route::get('/complaint/create','ComplaintController@create')->name('complaint.create');
Route::post('/complaint','ComplaintController@store')->name('complaint.store');
Route::get('/complaint','ComplaintController@index')->name('complaint.index');
Route::delete('/complaint/{$complaint->id}','ComplaintController@destroy')->name('complaint.destroy');

ComplaintController.php(基本控制器(:

class ComplaintController extends Controller
{
public function index() {
$complaints = Complaint::all();
return view('viewcomplaint',compact('complaints'));
}
public function create(User $user) {
$user = User::all();
$user->name = Auth::user()->name;
return view('createcomplaint',compact('user'));
}
public function store(Request $request, Complaint $complaint, User $user) {
$user =  User::find($user);
$complaint->name = Auth::user()->name;
$complaint->cname = $request->input('cname');
$complaint->cbody = $request->input('cbody');
//update whichever fields you need to be updated
$complaint->save();
return redirect()->route('home.index');
}
public function destroy(Complaint $complaint,$id)
{
$complaint = Complaint::findOrFail($complaint->id);
$complaint->delete();
return redirect()->route('complaint.index');
}
}

现在,当我单击表上的Delete按钮时,它只会给我"404|Not Found"错误。我在这里做错了什么?我真的很感激你的帮助。

从路由中删除$id

Route::delete('/complain/{id}','ComplaintController@destroy')->name('complaint.destroy');
public function destroy($id) {
}

route参数只是一个名称;你说这个特定的路段是动态的,我想要名为complaint:的参数

Route::delete('complaint/{complaint}', 'ComplaintController@destroy')->name('complaint.destroy');

然后,您可以调整destroy方法,将参数complainttypehinded作为Complaint $complaint,以获得隐式绑定:

public function destroy(Complaint $complaint)
{
$complaint->delete();
return redirect()->route('complaint.index');
}

在我看来,你定义的路线是错误的。将您的路线更改为:

Route::delete('/complaint/{id}','ComplaintController@destroy')->name('complaint.destroy');

您不需要在表单打开中使用array((,所以请将表单打开更改为:

{!! Form::open(['method' => 'DELETE', 'route' => ['complaint.destroy',$complaint->id]]) !!}

从提交按钮中删除$complaint->id,您不需要它。

现在,您在函数中所要做的就是找到具有您在表单中传递的idComplaint

public function destroy($id)
{
$complaint = Complaint::findOrFail($id);
$complaint->delete();
return redirect()->route('complaint.index');
}

如果你发现任何错误,请告诉我。

最新更新