Link to home
Start Free TrialLog in
Avatar of emi_sastra
emi_sastra

asked on

Understanding Laravel Function

Hi All,

I have below code.

 public function destroy(ManageUserRequest $request, User $user)
    {
        $this->userRepository->deleteById($user->id);

           $user->email = $user->email . '_deleted';
        // $user->deleted_at = now();
        $user->save();

        event(new UserDeleted($user));

        return redirect()->route('admin.auth.user.deleted')->withFlashSuccess(__('alerts.backend.users.deleted'));
    }

Open in new window


 public function deleteById($id) : bool
    {
        $this->unsetClauses();

        return $this->getById($id)->delete();
    }

Open in new window


 protected function unsetClauses()
    {
        $this->wheres = [];
        $this->whereIns = [];
        $this->scopes = [];
        $this->take = null;

        return $this;
    }

Open in new window

 
What is the purpose of unsetClauses ?

Thank you.
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

The clauses are additional parts to a query, such as the WHERE statement etc. When you no longer need these additional parts, you can call unsetClasuses and it will remove them so that the next time you run a query, they won't be applied. All that code does is set the various properties of $this to an empty array.
Avatar of emi_sastra
emi_sastra

ASKER

Hi Chris,

Should it be a mandatory, what if we omitted ?

Which query will be affected with/without the unsetClauses?

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Chris,

Great. I get it know.

Thank you.