douting1871 2018-08-12 10:02
浏览 63

使用Laravel中的路由删除我的数据库记录?

I'm using Laravel and I'm trying to achieve a delete function that takes care of deleting the selected task the user made.

I was wondering how can I achieve this using routes?

<a href="{{ route("tasks.edit", ["id" => $task->id]) }}"><button class="btn btn-outline-light">Edit</button></a> 
<a href="{{ route("tasks.destroy", ["id" => $task->id]) }}"><button class="btn btn-outline-light">Delete</button></a>

At this moment my delete route takes me to the same page as what the edit route does.

My delete function:

    public function destroy(Task $task)
{
    $task->delete();
    return redirect('/tasks')->with('success', 'Task removed');
}

I got a working delete function using the Laravel forms:

{!! Form::open(['action' => ['TasksController@destroy', $task->id], 'method' => 'POST', 'class'=> 'float-right']) !!}
 {{Form::hidden('_method','DELETE')}}
 {{Form::submit('Delete', ['class' => 'btn btn-outline-danger'])}}
{!! Form::close() !!} 

Is it worth to consider another method of taking care of my deletes or should I stick with using the Laravel forms?

  • 写回答

2条回答 默认 最新

  • doulai2025 2018-08-12 10:23
    关注

    That's because of CSRF protection. You may think using an ajax request. You can find an example in this post. How to delete record in laravel 5.3 using ajax request?

    评论

报告相同问题?