xxxxxxxxxx
/**
* Get the name of the "deleted by" column.
*
* @return string
*/
public function getDeletedByColumn()
{
return defined('static::DELETED_BY') ? static::DELETED_BY : 'deleted_by';
}
protected function runSoftDelete(){
$columns = [
$this->getDeletedAtColumn() => $this->fromDateTime($time),
$this->getDeletedByColumn() => Auth::id(),
];
}
xxxxxxxxxx
To also get soft deleted models :
$trashedAndNotTrashed = Model::withTrashed()->get();
Only soft deleted models in your results :
$onlySoftDeleted = Model::onlyTrashed()->get();
xxxxxxxxxx
//1:- add this colum in your migration
public function up()
{
Schema::table('users', function(Blueprint $table)
{
$table->softDeletes();
});
}
//2:- then on the relevent model add softDeletes trate;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model {
use SoftDeletes;
protected $dates = ['deleted_at'];
}
User::find(1)->delete(); //soft delete
User::find(1)->forceDelete(); //permenent delete
xxxxxxxxxx
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model {
use SoftDeletes;
protected $table = 'posts';
// ...
}
xxxxxxxxxx
$flights = Flight::where('active', 1)
->orderBy('name')
->take(10)
->get();