Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

saqueib's avatar

I want to use laravel permission with spark

I want to use laravel permissions, I can assign role on the register

Spark::createUsersWith(function ($request) {
    ...
    $user->assignRole('owner');
});

but how can I get the current role associated with a logged in user in front-end, and also I need to list all the roles available from roles table?

When I check in dev console user variable there is no roles property. and global spark.roles is also an empty array.

0 likes
3 replies
Cronix's avatar
$role = Auth::user()->roleOnCurrentTeam();
$roles = Spark::roles();
Cronix's avatar

Sorry, I misread what you were wanting. I'm not sure how to use laravel roles with spark, but there is a package that does and was made for Spark. https://github.com/ZiNETHQ/spark-roles

Spark.roles works for me in the fronend, but I'm using the built in spark roles system that you define in SparkServiceProvider. If you're not using that, you will probably need to add your role system to Spark to output the roles.

It doesn't look like there is a direct way to get the user role in Spark from the frontend. However, you can with some work.

You can use lodash (included w/spark) to search the Spark.state.currentTeam.users array for the current user by their id to get the role

var userRole = _.find(Spark.state.currentTeam.users, {id: Spark.userId}).pivot.role

But again, that will probably only work using the built-in Spark roles feature, and possibly the package I linked to above.

jaewun's avatar

To get any value regarding the currently logged in user from backend to front end, you just need to make sure it is appended to the json of your User model automatically, for example if you wanted to access a value called assigned_role, we could add to the appends property then access it in javascript via Spark.state.user.assigned_role.

Add a dynamic field to your User model and make use of the appends property to automatically add this to the json output of your User model.

protected $appends = ['assigned_role']

public function getAssignedRoleAttribute()
{
    return $this->howeverYouGetTheRoleHere();
}

Be aware that in most of these permissions libraries, you have have multiple roles per user.

1 like

Please or to participate in this conversation.