softdelete in laravel


student.php

model

use SoftDeletes;

migration


$table->timestamp('deleted_at')->nullable();

COntroller

 public function destroy(Request $request, $id)

    {

        $student = Student::find($id);


        if ($student) {

            $student->delete();


            return redirect()->back()->with('success', 'Student soft deleted successfully.');

        }


        return redirect()->back()->with('error', 'Student not found.');

    }




html

<form action="{{ route('student.destroy', ['student' => $data->id]) }}" method="POST">
@csrf
@method('DELETE')
<button type="submit">Soft Delete</button>
</form>


need the query results with softdelete

$models = YourModel::withTrashed()->get();


restore a soft deleted record 

$model = YourModel::withTrashed()->find($id);

$model->restore();

To restore a soft deleted record, you can use the restore method. Here's an example

$model = YourModel::withTrashed()->find($id);

$model->restore();


f you want to force delete a soft deleted record, removing it permanently from the table, you can use the forceDelete method. Here's an example:


$model = YourModel::withTrashed()->find($id); $model->forceDelete();











Department::where('id',$request['id'])->delete();

No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...