This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
I have created a model called Author which extends the RainLab\User\Models\User In the boot function extending form fields I only wish to display the tab fields if the user belongs to a specific RainLab\User\Models\UserGroup .
In this instance the group I want has the following fields
id =4, name = Author, code = author
I can achieve this by using the code below but was wondering if there was a better way of doing this.
UsersController::extendFormFields(function($form, $model, $context) {
if(!$model instanceof UserModel)
return;
if(!$model->exists)
return;
if(!AuthorModel::isAuthor($model))
return;
The function below is what I am using to check if user is a member of the author group .
public static function isAuthor($user)
{
return DB::table('users_groups')->where('user_id',$user->id)->where('user_group_id', 4 )->first();
}
Last updated
jamiegrand said: The function below is what I am using to check if user is a member of the author group . public static function isAuthor($user) { return DB::table('users_groups')->where('user_id',$user->id)->where('user_group_id', 4 )->first(); }
Something like this should work if relationships are correct:
return $user->whereHas(
'group', function($query) {
$query->where('code', 'author');
}
)->first();
Something like this should work.
return ! $model->groups->where('id', 4)->isEmpty()
or use the code
instead of id
in where clause.
Thanks Eoler and Renatio,
using 'groups' worked
return $user->whereHas( 'groups', function($query) { $query->where('code', 'author'); } )->first();
and the code below is nice
return ! $model->groups->where('id', 4)->isEmpty()
1-4 of 4