2017-03-23 4 views
1

Ich habe ein Problem mit Larave-Rahmenprogramm mit dem Löschen von CRUDS-Methode DELETE.Versuchen, Datensatz durch CRUD löschen route :: delete-Methode (Laravel 5.3)

Mein Weg Methode ist:

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

Meine Ansicht mit 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 Modell:

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

Wenn ich auf löschen Link bin klicken, dort ist ein Fehler wie folgt:

MethodNotAllowedHttpException in RouteCollection.php Zeile 233:

Ich weiß nicht, was los ist. Könntest du mir helfen, das Problem zu lösen?

Könnte mir jemand bei diesem Problem helfen? Ich wäre sehr dankbar, Grüße.

+1

'MethodNotAllowedHttpException' emittiert wird, wenn Sie eine Route zuzugreifen, die nicht hören für die Request-Methode aber nicht vorhanden Sie verwenden. Sie greifen nämlich über eine 'GET' Anfrage auf die' delete' Route zu. – apokryfos

Antwort

2

Mit Route::delete() können Sie es nicht in einem Anker platzieren. Machen Sie ein Formular mit DELETE Methode.

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

Dies ist mit der Anfrage zu tun, die Sie machen. Sie müssen entweder Formular mit dem Löschmethode erstellen, wie so

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

ODER Ihre Route

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

erhalten, um eine Änderung Wenn Sie das Formular Weg gehen nicht zu bekommen, für die {{csrf_field hinzuzufügen ()}}

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