【发布时间】:2020-03-16 00:03:22
【问题描述】:
所以我在表格中打印用户投诉,我还在每行打印一个删除按钮。当我单击该删除按钮时,我想从表中删除该特定投诉。我没有为此使用资源控制器,而是使用基本控制器。现在,这是我的代码:
ViewComplaint.blade.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');
}
}
现在,当我单击表格上的删除按钮时,它只会给我“404 | Not Found”错误。我在这里做错了什么?非常感谢您的帮助。
【问题讨论】:
-
您可以尝试从 destroy() 函数中删除 $id 参数并重试吗?
-
试过了,同样的错误
-
你使用的是什么版本的 Laravel?