skip to Main Content

I want to delete a record that has a foreign key from another table, but at the time of performing the action it leaves me by some restriction. I would like to know if there is a way to delete it without deleting the other record that is related.

public function destroy($id) {
        try {
            $exists = Orderdetail::where('id', $id)->exists();
            if (!$exists) {
                return response()->json(
                    [
                        'message' => 'Error al eliminar la orden',
                        'status'  => 'error',
                        'data'    => 'La orden por este id no existe',

                    ], 500
                );
            }

            // eliminar la orden
            $order = Orderdetail::findOrFail($id);
            $order->delete();


        } catch (Throwable $th) {
            return response()->json(
                [
                    'message' => 'Error al eliminar la orden',
                    'status'  => 'error',
                    'data'    => $th->getMessage(),

                ], 500
            );
        }
    }

2

Answers


  1. You might want to update the foreign key to have the ON DELETE SET NULL condition.

    Login or Signup to reply.
  2. In this case, you may to change the engine of your database from InnoDB to MyISAM . But be careful, using MyISAM that means No data integrity (e.g. relationship constraints) check, which then comes a responsibility and overhead of the database administrators and application developers.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search