how to call data from database to view by user_role laratrust - laravel

I use laratrust as my user role, in the user role there are 1 = superadministrator, 2 = administrator, 3 = perusahaan, then I want to display the names of existing perusahaan in my view, which means calling user = role id 3, then how to call it? I also don't really understand eloquent relationships

You need to get users in this way
// This will return the users with 'superadministrator' role.
$users = User::whereRoleIs('superadministrator')->get();
check the documentation

Related

Laravel Collection - put specific item on last position

I have database table with different roles and I need collection where items with role owner will be last. Here is database example:
ID
Role
1
admin
2
user
3
user
4
owner
5
user
How can I sort collection with those data to get item with role owner as last? I could only think of sorting it alphabetically, saving to some variable and deleting the record with the owner role from the collection, and then pasting it into the collection as last. But it seems unnecessarily complicated to me, isn't there an easier way?
The reason why I need this is because I need remove all selected users and to be sure that user/owner is the last one.
You Can Use Conditional rendering. For That you need to use DB::raw
Example:
Model::select('id', 'role')
->orderBy(DB::raw('role = "owner"'), 'ASC');
Or Use shorthand:
Model::select('id', 'role')->orderByRaw("role = 'owner' ASC");
It will place owners At the end.
Check Fiddle here: http://sqlfiddle.com/#!9/9d2b64/2
For eloquent, you can use sortBy() method with array_search() :
$collection = Model::get();
$data = $collection->sortBy(function($item){
return array_search($item->role, ['admin', 'user', 'owner']);
});

Add records to pivot table

i have 3 tables.
table users
id
name
table identifiers
id
email
and pivot table identifier_user
identifier_id
user_id
when store data I when to add data to the pivot table, example :
$user=new User ;
$iden=new Identifier ;
I use $user->identifiers()->attach($user->id) , but the one just add the data to identifier_id but i want to add data to user_id to
NB: I made the relationship ManeytoManey in the two models
First of all: you cannot do your task like in question, cause $user = new User; only creates default User class instance.
For example: if you doesn't have a user, you need to create it like this:
$user = User::create($userData);
But if you already has user in database:
$user = User::findOrFail($userId); or $user = User::where(['some_field' => $some_value])->first();
After that, you can attach relation:
$user->identifiers()->attach([$identifier_ids]);
if you already know relation ids, or, if you need to create it:
$user->identifiers()->([
new Identifier($identifierDataArray),
new Identifier($identifierDataArray),
]);
Hope it helps you.

Customize Passport Queries time of authenticating

For every request I found that 4 queries are fired to validate the user and the token. Among them one is to fetch the user (select * from user) based on the user id. This queries are fired by Passport/Laravel But what I want is to modify this query to add one status field check also to check if any user become invalid during the token validity period. If we only check with the id then if any user become inactive(By changing status then also we will not be able to stop the user as deleting the token for the user is not a good solution for me).
Queries Fired on every request by Passport Laravel:
select * from oauth_access_tokens where id = ?
select * from user where id = ? limit 1 ["2"]
select * from oauth_access_tokens where id = ?
select * from oauth_clients where id = ?
So, can anyone tell me how to change the 'select * from user where id' query in passport at time of Token validation.
You can add this method on your User model (or any model you're authenticating with passport)
...
public function findForPassport($username)
{
return $user = (new self)->where('email', $username)->where('is_active', 1)->first();
}
...
of course you can modify is_active by whichever column you are using (and/or any query constraint for that matter), as long as it returns Illuminate\Contracts\Auth\Authenticatable contract.
I wouldn't try and modify passports default behaviour as I have no idea what else it might impact both now and in future upgrades.
Your best bet might be to hook into the passport events and apply you business logic to a listener that is called when the events are fired

Filtering resultset with codeigniter and datamapper

I got a little problem with datamapper, and I'd like to know if there is a quick solution.
Let's say I have this kind of data
Groups table
id | Name
1 | admin
2 | guest
3 | editor
4 | moderator
In my base controller I set a global field to see only the groups that are not admin
$this->groups_ = new Group();
$this->groups_->where('id >', 1)->get();
//so I can select the users that are not admin
$users = new User();
$users->where_related('group',$id,$this->groups_)->get();
Now in my controllers I'd like to filter the groups. For example I want to select only editors and guests (id between 1 and 4). So I would like to filter the initial result set... something like this
$this->groups_->where('id <',4)->get();
But it doesn't work. It returns ALL the group ids < 4 including admin.
What would be the right way to get this?
Well, datamapper does not query objects or groups of objects. It queries the database. So when you do the second get(), you're executing a separate query on the database.
You can do this, but it will be an additional query:
$this->groups_->where('id >', 1)->where('id <', 4)->get();
or you can loop through $this->groups_ after your first query in php and filter the set there and save as an array.
A possible solution
Maybe I found a possible workaround by cloning the datamapper object.
What I want to do is not to query the db multiple times, but I'd like to have a base resultset and refine the results.
So in my base controller I would do something like
$this->groups_ = new Group();
$this->groups_->where('id >', 1);//without getting the results
//so I can select the users that are not admin
$users = new User();
//here i'm getting the results from the clone
$users->where_related('group',$id,$this->groups_->get_clone()->get())->get();
And, as I left the object "open", without getting the results, I can use it in my controllers, as a base for my other "queries"... ok,they are actually new conditions of the first query.
//now the query return the groups between 2, as I set in the base controller
//and 4, set in the child controller
$this->groups_->where('id <',4)->get();

How to delete all roles of a list of users using membership. -MVC

I have a list of username's in a string array . l wanna give the all selected users a new role .I can easily give that using membership
But before doing that i want remove all roles assigned to selected users. How can i do that ..
or at least how can i remove all roles assigned to a particular user?
Roles.RemoveUserFromRoles(user.UserName, Roles.GetRolesForUser(user.UserName));
Try this for one user
Roles.RemoveUserFromRoles
or
Roles.RemoveUsersFromRoles
for many.
Here is a good tutorial about Roles, http://www.asp.net/security/tutorials/assigning-roles-to-users-cs
I know its old...but I was just looking for this myself:
MembershipUser mUser = Membership.GetUser((Guid)User.UserID);
foreach (var role in Roles.GetAllRoles())
{
Roles.RemoveUserFromRole(mUser.UserName, role);
}

Resources