Laravel. Deletion with a condition - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Tuesday, July 18, 2023

Laravel. Deletion with a condition

Good afternoon. Please help to perform the removal according to SOLID. While employees are in the office, the office cannot be deleted.

Now the code is simple. How to change it to a more correct one?

public function destroy(int $id): JsonResponse
    {
        $this->countUser = User::where('office_id', $id)->count();
        if ($this->countUser > 0) {
            $this->response = [
                'message' => 'office not remove!',
            ];

            return response()->error($this->response);
        }
        $this->item = Model::findOrFail($id);
        $this->item->forceDelete();
        $this->response = [
            'message' => 'office remove!',
            'reload' => true,
        ];

        return response()->success($this->response);
    }


source https://stackoverflow.com/questions/76707137/laravel-deletion-with-a-condition

No comments:

Post a Comment