-1

I just started using soft delete and I am not really sure how to do it, I am just following someone example and is still unsure how to do a proper soft delete. All I want is to delete the personal_info table but I keep on getting a no error message which make me at lost since I don't know what I am doing wrong, can someone help me? Thanks a lot

home.blade.php

    <table class="table table-bordered">
      <tr>
        <th><strong><big>Name: </big></strong></th>
        <th><strong><big>Action </big></strong></th>
      </tr>
      <td>
      <tr>
        @foreach($data as $value)
      <tr>    
      <th><a href="{{route('user.show',['id'=>$value->id])}}">{{$value->Name}}</a></th>
      <th><form action="{{  url('/home/'.$value->id.'/delete')  }}" method="post">
        <button>Delete</button>
      </form></th>               
      </tr>
        @endforeach
      </tr>
      </tr>
    </table>

Controller:

public function delete($id = 0){   
    if ($id > 0){
        personal_info::destroy($id);
    }
    return redirect("/home");
}

or should I do it this way?

public function delete($id){
$data = personal_info::find($id)->delete();
    return redirect("/home");
}

personal_info model:

use Illuminate\Database\Eloquent\Model;

use Eloquent;
use SoftDeletes;
class personal_info extends Eloquent
{
    protected $fillable = array('Name');
    protected $table = 'personal_infos';
    protected $primaryKey = 'id';
    protected $dates = ['deleted_at'];
        public function user_info1s() {
        return $this->hasMany('App\user_info1','user_id');
    }

Route: (not sure should I use DELETE instead)

Route::get('/home/{id}/delete', 'HomeController@delete');

enter image description here

blastme
  • 391
  • 7
  • 19
  • 37

2 Answers2

1

There is no need to use delete on the method. try to change your function to this

public function delete($id){   
    personal_info::findOrFail($id)->delete();
    return back();
}

Edit

The Route method and Form method must be the same.

Onix
  • 2,129
  • 2
  • 17
  • 26
1

Change

Route::get('/home/{id}/delete', 'HomeController@delete');

To

Route::post('/home/{id}/delete', 'HomeController@delete');

Controller Method

use Carbon\Carbon;

public function delete($id)
{
  personal_info::find($id)->update([
      'deleted_at' => Carbon::now()
  ]);

  return redirect()->back();
}

home.blade.php

<table class="table table-bordered">
      <tr>
        <th><strong><big>Name: </big></strong></th>
        <th><strong><big>Action </big></strong></th>
      </tr>
      <td>
      <tr>

        // Example of adjusting your query to support soft deletes
        @php
          $data = Some\Model::whereNotNull('deleted_at')->get();
        @endphp

        @foreach($data as $value)
      <tr>    
      <th><a href="{{route('user.show',['id'=>$value->id])}}">{{$value->Name}}</a></th>
      <th><form action="{{  url('/home/'.$value->id.'/delete')  }}" method="post">
        <button>Delete</button>
      </form></th>               
      </tr>
        @endforeach
      </tr>
      </tr>
    </table>