简体   繁体   English

尝试通过CRUD route :: delete方法删除记录(Laravel 5.3)

[英]Trying to delete record by CRUD route::delete method(Laravel 5.3)

I have a problem with Larave's framework program with deleting by CRUDS method DELETE. 我对Larave的框架程序有疑问,可以通过CRUDS方法DELETE删除。

My route method is: 我的路线方法是:

 Route::delete('cats/{cat}/delete', function(Furbook\Cat $cat){
 $cat->delete();    return redirect('cats.index')       
 ->withSuccess('Cat
 has been deleted.'); });

My view with delete url: 我的删除网址视图:

@extends('layouts.master')

@section('header')
<a href="{{ url('/') }}">Back to the overview</a>
<h2>
    {{ $cat->name }}

</h2>
<a href="{{ url('cats/'.$cat->id.'/edit') }}">
    <span class = "glyphicon glyphicon-edit"></span>
    Edit    
</a>
<a href ="{{ url('cats/'.$cat->id.'/delete') }}">
    <span class ="glyphicon glyphicon-trash"></span>
    Delete
</a>
<p>Last edited: {{ $cat->updated_at }}</p>
@endsection

@section('content')
<p>Date of Birth: {{ $cat->date_of_birth }} </p>
<p>
    @if ($cat->breed)
    Breed:
     {{ url('cats/breeds/'.$cat->breed->name) }} 
    @endif

</p>
@endsection

My Cat model: 我的猫模型:

<?php 

namespace Furbook;

use Illuminate\Database\Eloquent\Model;

class Cat extends Model {
    // We specified the fields that are fillable in the Cat model beforehand
    protected $fillable = ['name','date_of_birth','breed_id'];
    // informacja o tym, żeby nie uaktualniać update_at w tabeli kotów
    public $timestamps = false;
    public function breed(){

        return $this->belongsTo('Furbook\Breed');
    }
}
?>

When I'm clicking on delete link, there is an error like this: 当我单击删除链接时,出现如下错误:

MethodNotAllowedHttpException in RouteCollection.php line 233: RouteCollection.php第233行中的MethodNotAllowedHttpException:

I don't know what's wrong. 我不知道怎么了 Could you somoene help me with solving problem? 你能帮助我解决问题吗?

Could someone help me with this problem? 有人可以帮我解决这个问题吗? I would be very grateful, greetings. 问候,我将非常感谢。

This is to do with the Request you are making. 这与您发出的请求有关。 You must either create form with the delete method like so 您必须像这样使用delete方法创建表单

<form action="{{ url('cats/'.$cat->id.'/delete') }}" method="DELETE">
    <button class ="glyphicon glyphicon-trash">Delete</button>
</form>

OR change your route to a get 或更改路线以获得

Route::get('cats/{cat}/delete', function(Furbook\Cat $cat){
     $cat->delete();    
     return redirect('cats.index')->withSuccess('Cat has been deleted.');      
});

If you go the form route don't for get to add the {{ csrf_field() }} 如果您使用表单路线,请不要添加{{csrf_field()}}

https://laravel.com/docs/5.4/csrf https://laravel.com/docs/5.4/csrf

Using Route::delete() , you cannot place it in an anchor. 使用Route::delete() ,无法将其放置在锚点中。 Make a form with DELETE method. DELETE方法制作表格。

{!! Form::model($cat, ['method' => 'DELETE', 'url' => 'cats/'.$cat->id.'/delete']) !!}
    <button type="submit">Delete</a>
{!! Form::close() !!}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM