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

时间:2017-03-23 10:42:38

标签: php laravel

我对Larave的框架程序有问题,删除了CRUDS方法DELETE。

我的路线方法是:

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

我的删除网址视图:

@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

我的猫模型:

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

当我点击删除链接时,会出现如下错误:

RouteCollection.php第233行中的MethodNotAllowedHttpException:

我不知道出了什么问题。你可以帮助我解决问题吗?

有人可以帮我解决这个问题吗? 我会非常感激,问候。

2 个答案:

答案 0 :(得分:3)

这与您正在制作的请求有关。您必须使用delete方法创建表单,如此

Index exceeds matrix dimensions.

或将您的路线更改为获取

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

如果你去表格路线,请不要添加{{csrf_field()}}

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

答案 1 :(得分:2)

使用Route::delete(),您无法将其放入锚点。使用DELETE方法制作表单。

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