0

I have a form associated with the destroy method to delete a item/record.

<form action="{{ route('climb-excluded.destroy',$exclude->id) }}" method="POST">
    <input type="hidden" name="_method" value="DELETE">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <button type="submit" class="btn btn-danger btn-sm">
        <i class="fa fa-trash-o" aria-hidden="true"></i> Delete
    </button>
</form>

destroy() method

<?php

public function destroy(Cexcluded $cexcluded)
{
    $cexcluded->tours()->detach();
    $cexcluded ->delete();
    Session::flash('success','Item sucessfully deleted !');
    return redirect()->route('climb.ie');
}

Routes

 Route::resource('climb-excluded','CexcludedsController', ['only'=>['store','destroy']]);
 Route::get('climbing/included-excluded','HomeController@getclimbIE')->name('climb.ie');  

The trouble I'm having is the destroy method is not deleting the record from the DB and doesn't give any error. It is giving a session message without deleting the record.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
psudo
  • 1,341
  • 3
  • 24
  • 69

2 Answers2

0

I saw your other thread with the same issue regarding the detach causing problems.

Add this to your Cexcluded model

public static function boot()
{
    parent::boot();

    static::deleting(function($model) {
        $model->tours()->detach();
    });
}

Remove the following line from your controller method

$cexcluded->tours()->detach();

So try this

public function destroy(Cexcluded $cexcluded)
{
    $cexcluded ->delete();
    Session::flash('success','Item sucessfully deleted !');
    return redirect()->route('climb.ie');
}
Sandeesh
  • 11,486
  • 3
  • 31
  • 42
  • Thank you for your suggestion. But it didn't work as well. So I changed my destroy method to old conventional way.`public function destroy($id) { $cexcluded = Cexcluded::find($id); $cexcluded->tours()->detach(); $cexcluded ->delete(); Session::flash('success','Item sucessfully deleted !'); return redirect()->route('climb.ie'); }` – psudo May 22 '17 at 10:58
  • Really weird that detaching relationships is causing you issues. Anyway good that you got it work. – Sandeesh May 22 '17 at 11:29
0

You must use "->destroy()" in the controller. Check this question for clarification between destroy() and delete()

Community
  • 1
  • 1
Hex
  • 1
  • 1
  • 4