lundi 6 février 2023

forceDelete method not working as expected in laravel 5 project (can't delete model from the database)

I working (editing) on project base on laravel 5.8 in the database I have "contacts" table main columns are :

( id ,name, .. ,deleted_at , deleted_by agency_id contactable_id ... )

so when is use $model->delete() for soft delete it will work very well

before delete ( id :1 ,name: 'name', .. ,deleted_at : null , deleted_by :null agency_id : 1 contactable_id : 1)

after delete ( id :1 ,name: 'name', .. ,deleted_at : (dateValue) , deleted_by :1 agency_id : 1 contactable_id : 1)

class ContactController extends Controller
{
 public function delete(Request $request)
    {


        if ($request->ajax()) {
            \Log::info($request->contact_id);
            try {
                $deleted = $this->model_instance::find($request->contact_id)->delete();
            } catch (\Exception $ex) {
                \Log::info($ex->getMessage());
            }
            if ($deleted) {
                $log_message = trans('contacts.delete_log') . '#' . $request->contact_id;
                //logActivity($log_message);
                return response()->json(['status' => 'success', 'message' => 'deleted_successfully']);
            } else {
                return response()->json(['status' => 'fail', 'message' => 'fail_while_delete']);
            }
        }

        return redirect()->route($this->index_route);
    }
}

so when is use $model->forceDelete() for delete for ever from the database (forceDelete) it will not work as expected but will fill deleted_by with user number and keep deleted_at with null value

before force delete ( id :1 ,name: 'name', .. ,deleted_at : null , deleted_by :null agency_id : 1 contactable_id : 1)

after force delete ( id :1 ,name: 'name', .. ,deleted_at : null , deleted_by :1 agency_id : 1 contactable_id : 1)

class ContactController extends Controller
{
    public function forceDelete(Request $request)
    {
        if ($request->ajax()) {
            \Log::info($request->contact_id);
            try {
                $deleted = $this->model_instance::withTrashed()->find($request->contact_id)->forceDelete();
              
            } catch (\Exception $ex) {
                \Log::info($ex->getMessage());
            }
            // if ($deleted) {
            //     $log_message = trans('contacts.delete_log') . '#' . $request->contact_id;
            //     return response()->json(['status' => 'success', 'message' => 'deleted_successfully']);
            // } else {
            //     return response()->json(['status' => 'fail', 'message' => 'fail_while_delete']);
            // }
        }
        // return redirect()->route($this->index_route);
    }
}

forceDelete() return true


so this method work like this or there problem in my code in some where(I mean not jsut the previos code ) ?!

use softdelete in some cases use delete (for soft delete) and other cases use forceDelete (for delete from the database)


for soft delete it worked very well for force delete it takes another scenario



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire