Search code examples
codeignitercodeigniter-4

How to update a user using the 'Ion Auth' authentication library in CodeIgniter 4 framework


Below is my code snippet. I want to reduce the balance of a user in the database using Codeigniter 4?

dd($this->ionAuth->update($this->ionAuth->users()->row()->id, ['balance' => 1000]));

*I'm using Ion Auth for updating. It's just a big burden when updating the database using modelling data.


Solution

  • It looks like you intend to use $this->ion_auth->user() instead of $this->ion_auth->users().

    Also keep in mind that executing a dump and die on $this->ion_auth->update(...) returns a boolean not the updated user data.

    References:

    users() gets all users.

    users()
    Get the users.

    Parameters

    'Group IDs, group names, or group IDs and names' - array OPTIONAL. If an array of group ids, of group names, or of group ids and names are passed (or a single group id or name) this will return the users in those groups.

    Usage

    $users = $this->ion_auth->users()->result(); // get all users
    

    user() gets a single user.

    user()
    Get a user.

    Parameters

    'Id' - integer OPTIONAL. If a user id is not passed the id of the currently logged in user will be used.

    Usage

    $user = $this->ion_auth->user()->row();
    echo $user->email;