whats the best or the usual way to remove all roles from a user?
I tried
$roles = $user->getRoleNames(); $user->removeRole($roles);
Return value of App\User::getStoredRole() must implement interface Spatie\Permission\Contracts\Role, instance of Illuminate\Support\Collection returned
Use the plain Laravel detach method like so:
$user->roles()->detach();
I dod it now in this way $user->removeRole($user->roles->first());
You can also remove all roles by syncing to an empty array, like so.
$user->syncRoles([]);
I confirmed it works on version 5.8.
From reading the documentation it clearly says that you can pass a Collection instance to the removeRole so I think you are doing it right.
The assignRole, hasRole, hasAnyRole, hasAllRoles and removeRole functions can accept a string, a \Spatie\Permission\Models\Role object or an \Illuminate\Support\Collection object.
This works fine even on Laravel 7
For Roles:
$user->syncRoles([]);
For Permissions:
$user->syncPermissions([]);
From Spatie documentation you can find ther is a way to remove all previous roles and assign new roles with simple
$user->syncRoles($roles);
For reference you can visit this link
Related
I'm new to Laravel I'm making a clone of Twitter. I'm making a scope to get all the likes from the DB, but I get an error from Tinker
I know some basic SQL Queries, but this one is quite complicated, so I've got no idea what to do now.
Tweet model
public function scopeWithLikes(Builder $query)
{
$query->leftJoinSub(
'select tweet_id, sum(liked) likes, sum(!liked) dislikes from likes group by tweet_id',
'likes',
'likes.tweet_id',
'tweet.id'
);
}
Tinker command
App\Tweet::withLikes()->first();
Tinker error
TypeError: Argument 1 passed to App/Tweet::scopeWithLikes() must be an
instance of Illuminate/Database/Query/Builder, instance of
Illuminate/Database/Eloquent/Builder given, called in
C:/wamp64/www/laravel/tweety/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
on line 1164
I hope I explained it well, but if you need more information please ask me.
Thanks for your answer!
i think that you are using the wrong class for your scope, scope use
Illuminate/Database/Query/Builder
as a parameter while you pass
Illuminate/Database/Eloquent/Builder
in your Twit model file, on the top ...
remove:
use Illuminate/Database/Eloquent/Builder;
and paste:
use Illuminate/Database/Query/Builder;
Did this solve the problem? Because I run into exactly the same issue.
When I change Eloquent to Query, the error message is still the same.
Also the source that is given on Git uses use
Illuminate\Database\Eloquent\Builder;
Hubert
In eXist 4.4 I am setting a user's rwx permission in XQuery using sm:add-user-ace(). I do not see a corollary 'remove-user-ace', so I assume I have to use sm:remove-ace($path as xs:anyURI, $index as xs:int). However this requires knowing the $index. Is there function for getting the $index for a combination $user and collection/resource? (I don't see any at the eXist security fundocs)
Many thanks in advance.
You need to use the sm:get-permissions function on the URI of the Document or Collection. It will give you an XML document which will show all the ACL entries.
I was using this function in 5.3 and now when I try to use it in 5.4 like this:
$post->tags()->getRelatedIds();
I am getting errors that function does not exist, I checked the documentation for 5.4 and it's not there anymore.
Anyone knows why this usefull function was removed and what I can do to get all ids from related model?
In Laravel 5.4 and 5.5 the getRelatedIds is replaced by allRelatedIds.
$post->tags()->allRelatedIds();
I can't speak for reasons why it has been removed but if you know the primary key name ahead of time (i.e. all your tables have an id column) you can simply do
$post->tags()->select('id')->pluck('id');
if you want a more generic way you'd need to jump through some hoops
$related = $post->tags();
$post->tags()
->select($related->getQualifiedKeyName())
->pluck($related->getKeyName());
I'm trying to find all users w/ a specific permissions list in Sentry with laravel. The problem is that Sentry::findAllUsersWithAccess() returns an array().
as stated in their github repository i pinpointed their code to be
public function findAllWithAccess($permissions)
{
return array_filter($this->findAll(), function($user) use ($permissions)
{
return $user->hasAccess($permissions);
});
}
right now, it gets all users and filter it out with users with permission list. the big problem would be when I as a developer would get the set of users, it'll show ALL users, i'm developing an app which may hold thousands of users and i only need to get users with sepcific permission lists.
With regards to that would love to use one with a ->paginate() capability.
Any thoughts how to get it without getting all the users.
Why dont you override the findAllWithAccess() method and write your own implementation, which uses mysql where instead of array_filter().
I dont know your project structure and the underlying db schema, so all i can give you atm is the link to the eloquent documentation Querying Relations (whereHas).
In case you dont know where to start: its always a good idea to look at the ServiceProvider (SentryServiceProvider, where the UserProvider, which holds the findAllWidthAccess() method, is registered). Override the registerUserProvider method and return your own implementation of the UserProvider (with the edited findAllWithAccess() method).
Hope that will point you in the right direction.
In Laravel you can do pagination manually on arrays:
$paginator = Paginator::make($items, $totalItems, $perPage);
Check the docs: http://laravel.com/docs/pagination
Since I'm porting an app to Laravel and it's using the Auth Class, I need to change all the passwords in my users table to bycrypt (using Hash::make()).
The thing is that I want to use the usernames as default password (so when the migration is done, my user "Mario" will have a Password of "Mario") — I wanna do this with all the entries of the database via a Migration, but I can't seem to make it, since I don't know how to get the value of the select, hash it, then use it in the update.
Is there any way to do this without using loops? (i.e without making one query per user)
EDIT: Yes, this is impossible to do without loops. I realized that. And #Adrenaxus has the right answer.
Why don't you do something like this:
foreach(User::all() as $user){
$user->password = Hash::make($user->username);
$user->save();
}