这是我的代码
视图
<form action="/categories/ {{ $category->id }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button>Delete Task</button>
</form>
控制器方法
public function destroyCategory($id)
{
Categories::findOrFail($id)->delete();
return redirect('/categories');
}
路线
Route::post('categories','FilesController@destroyCategory');
表单是部分视图,可能是个问题
答案 0 :(得分:2)
您从表单中发送了DELETE
方法,但在post
文件&amp; delete
文件中将其作为route
代替URL
。您也不要在route
文件中指定<form action="/categories/{{ $category->id }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit">Delete Task</button>
</form>
参数。
Route::delete('categories/{id}','FilesController@destroyCategory');
<强> route.php 强>
select to_char(SCHEDULED_DATE, 'mon-yyyy'), count(*) FROM SCHEDULE
--optionally to get only some months
where to_char(SCHEDULED_DATE, 'mon-yyyy') in (/*fill values from your array here*/)
and seq = '439'
group by to_char(SCHEDULED_DATE, 'mon-yyyy');
答案 1 :(得分:0)
/categories
之后你有空格而你没有使用提交按钮:
<form action="/categories/{{ $category->id }}" method="POST">
{{ csrf_field() }}
<input name="_method" type="hidden" value="DELETE">
<input class="btn btn-danger" type="submit" value="Delete Task">
</form>
或使用Laravel Collective表格:
{!! Form::open(['method' => 'Delete', 'route' => ['task.delete', $category->id]) !!}
{!! Form::submit('Delete Task') !!}
{!! Form::close() !!}
此外,您使用post
路线代替delete
。而且,正如@Rimon Khan所说,你没有抓住一个类别的ID:
Route::delete('categories/{id}', ['as' => 'task.delete', 'uses' => 'FilesController@destroyCategory']);
答案 2 :(得分:0)
你可以这样做:
您的路线应该像:
Route::post('categories/{id}','FilesController@destroyCategory')->name('delete_categories');
您的控制器应该像:
public function destroyCategory($id)
{
Categories::findOrFail($id)->delete();
return redirect()->to('/');
}
和HTML将是这样的:
<form action="{{ route('delete_categories', ['id' => $category->id]) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit">Delete Task</button>
</form>
希望这有帮助