soft deleted in laravel 5 apply physicaldelete not set deleted_at -
my model is:
<?php namespace app\http\models; use illuminate\database\eloquent\model; use illuminate\database\eloquent\softdeletes; class province extends model { // use softdeletes; public $timestamps = false; protected $dates = ['deleted_at']; public function country(){ return $this->belongsto('app\http\models\country'); } public function users(){ return $this->hasmany('app\http\models\user'); } public function customers(){ return $this->hasmany('app\http\models\customer'); } }
controller:
public function destroy($id) { $province = province::with('country')->where('id', $id)->first(); // dd($province); $province->delete(); }
when call http://localhost:8080/pal/public/province/22/delete.
record 22 deleted physically database.
what's missing?
when models have soft delete enabled, records deleted_at
not null
won't returned when make query. therefore try call delete()
on null
, causes error.
set deleted_at manually null
"undelete" record during testing.
furthermore should check if model null
before deleting:
$province = province::with('country')->where('id', $id)->first(); if($province != null){ $province->delete(); }
Comments
Post a Comment