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

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.

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;

look here authentcate library for ci4
https://github.com/lonnieezell/myth-auth
go there download it
ionAuth is not compablitablke with ci4

Related

Laravel scope query only for users - exclude result for admin

I am very new into programming and trying to make some work on Laravel / Voyager.
I have a Customers table and on this table there is also Customer_Representative field where I can select / assign customer representatives from dropdown list.
What I am trying to achieve is I want each users with their assigned roles (Customer Representative) to list customers only assigned for them.
I am using this below code, but I am not able to see list of customers that other users created. I can only see the customers I / Admin created but If I login with their details I can see their assigned customers.
So as an admin I want to be able to see all customers, so I can also edit them with my login credentials.
public function scopeCurrentUser($query)
{
return $query->where('Customer_Representative', Auth::user()->id);
}
I saw many suggested to use if but I couldn't figure out how to do it.
Appreciate your supports !
If I understand correctly, you want to display:
admin users - all users
non-admin users - only assigned users
You can either change the query in controller, but if you want to do it in scope I suggest something like this:
public function scopeCurrentUser($query)
{
return $query->when(auth()->user()->is_not_admin, function ($q) {
$q->where('Customer_Representative', auth()->user()->id)
});
}
You change the auth()->user()->is_not_admin to your condition for non-admin validation. The scope utilizes the ->when() function to append the condition to query only if the condition (in this case if authenticated user is not an admin) is true.
So if authenticated user is not an admin, his query will be filtered by Customer_Representative.

Facing issue in laravel orderby

I am facing a issue with laravel order by function.
i want to fetch the records according to country and the result should start from the current user's country.
For example.
if auth user belong to india then list should start from india.
My Current code is
$users = User::where('status', '=',1)->with(['profile'])->latest()->orderBy('last_login', 'desc')->paginate(12);
Thanks
Ok, whatever I've understood, you are looking to sort the data through the country attributes from profiles table, which is in relation with User model, for this you need to use join I'm not sure about your structure so I'm just giving you an example. Suppose you want to sort by profiles => id then you can write something:
Edit: As per the requirement given in comments I would recommend to have something like this:
First of all lets have authenticated user by
$AuthUser = Auth::user();
Then lets call the relation
$users = User::->leftJoinRelations('profile')
->orderByRaw("FIELD(country , ". $AuthUser->profile->country ."), ASC")
->paginate();
So in this way you can have user sorted by authenticated user country. Hope this helps.

Retrieve selected keys from database from relations

I am using laravel 4 and I have to retrieve registrations and all users associated with that registrations but user table has lot more keys but I only need 3 keys so how can I get selected keys. I know laravel provide select method but its not working on relations.
Registration::where('section_id', '=', $key->id)
->with('user')
->select('users.id', 'users.name')
->get();
I tried users and user but nothing works. Could someone please post correct way to load this query.
Edit:
- Registration has one user
- Registrations has one section
I have registration table that contains section id and user id. I want to get all users, signed up for particular section. So this is very simple query and its working fine only problem is retrieving selected keys from database.
In your Registration model change your relation to this
public function user()
{
return $this->hasOne('User')->select('id','name');
}
And in your controller write this
Registration::where('section_id', '=', $key->id)->with('user')->get();
Hope this work for you.

Laravel passing variable from multiple models

I have 2 models
User
Customer
Each user hasMany customers (aka Accounts)
the user model has
public function customer() {
return $this->hasMany('App\Customer', 'id','customer_id');
}
but when i try to output this:
$user->customer->name
i get an error saying:
Undefined property: Illuminate\Database\Eloquent\Collection::$name
When i use this one, i get the entire customers record output in JSON.
$user->customer
So the relationship is working, but obviously i am missing something. I was sure this was the right way to do it. I swear this worked in Laravel4.2 but now that I'm in Laravel 5 it doesn't.
As the error and the below link says, It is returning a collection (hasMany) not a single object, you need to either loop over the collection or do direct access in the collection based on the index.
that is to say... does doing this work?..
$user->customer[0]->name
http://laravel.io/forum/04-10-2014-undefined-property-illuminatedatabaseeloquentcollectionitem
You should loop over customers, if you want display customers in laravel view you can do the following
#foreach
($user->customer as $cust)
{{$cust->name}}
#endforeach

Laravel Sentry limits

Anybody know how to get limited number of sentry users. I have admin panel in my laravel application and I make a user management. For display data (groups table, user table, permission table, ...) I use jqgrid. So if I have half million users I want to get limited list of users with specific data.
For use limitation in Laravel, I use standard function skip and take, but I cannot use these method on Sentry findAllUsers().
What solution is the best in this case?
There are some ways, one of them is to get an empty Sentry user model, which is based on Eloquent:
$model = Sentry::getUserProvider()->getEmptyUser();
And use it as you would with Eloquent:
$all = $model->all();
$paginated = $model->paginate();
$selected = $model->where('age', '<', 21)->get();

Resources