add soft delete in laravel

In the migration file add:

$table->softDeletes();

In the model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;


class CalNotes extends Model
{
    use HasFactory;
    protected $table = 'cal_notes';
    protected $fillable = ['user_id','title','description','price','ip'];
    protected $primaryKey = 'id';
    use SoftDeletes;

}

In the controller:

 $id = $request->id;
 $calNotesModel  = new CalNotes();
 $calNotes = $calNotesModel->find($id);
 $calNotes->delete();

 if you need to restore the deleted user

$calNotes = CalNotes::withTrashed()->find($id);
        $calNotes->restore();



Leave a Reply