Laravel删除按钮抛出错误“页面无法找到

时间:2016-11-08 12:32:16

标签: php laravel laravel-5 laravel-5.2

这是我的代码

视图

<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');

表单是部分视图,可能是个问题

3 个答案:

答案 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>

希望这有帮助