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

ajsmith_codes's avatar

Need help with logic and permissions.

I have an app where I am using Spatie Permissions. If you're not familiar with it, it is where you can assign Permissions and Roles. They recommend you assign permissions to roles instead of users. I am not certain that will work for my situation and need some advice/guidance.

Here is what I'm trying to do:

John owns Project 123. He wants to allow Jane to edit the project. He wants Jim to have view-only rights.

When he invites them, the pivot table called project_members gets an entry that has the project_id, user_id, and permission_id. (basically a pivot table linking to three other tables)

Permission_id of 1 is "edit projects".

Permission_id of 2 is "view only".

How do I make use of this by using one view instead of two (if two, one is read-only and second is edit)?

If I were to use Roles or Permissions alone, I could just say @can('manage'). However, with the way I have it set up, that doesn't work.

Hopefully, this makes sense.

0 likes
18 replies
bugsysha's avatar

Either return different views/routes for those two cases, or use something like includeWhen blade directive to which you can pass a condition which will dynamically include files.

@includeWhen($boolean, 'view.name', ['some' => 'data'])
ajsmith_codes's avatar

I will look into that.

How would I go about getting a boolean for this situation:

Referencing the pivot table, I would check that the user is a member of the project, but then I need find out if they have edit permissions.

Is this close? I can't seem to get the logic working.

    $user = auth()->user();
    
    $members = $project->members();

    $canEdit = $members->where('permission_id', 1)->where('user_id', $user->id);
bugsysha's avatar

Never used that package. I do not see any value in packages like that one. Overcomplication of a simple problem.

ajsmith_codes's avatar

Pretend I don't use it. How would you go about giving a user permission to edit a specific project? I don't want them to edit all projects.

ajsmith_codes's avatar

I think I'm going to do a mix of that and what I have. Thanks!

ajsmith_codes's avatar

Sorry, another question about this. How would I find out if the user can update the project? I need to look in a pivot table, make sure the user is a member of that project, then pull true or false from a column called 'can_edit'.

bugsysha's avatar

I guess something like this:

class ProjectPolicy
{
	public function update(User $user, Project $project): bool
	{
		return $project->members()
			->where('user_id', $user->id)
			->where('permission_id', Permission::UPDATE)
			->exists();
	}
}
ajsmith_codes's avatar

I'm trying that, but it's not working. I'll keep looking at my controller and see if I can figure it out. Thanks!

ajsmith_codes's avatar

Here is my policy:

public static function editProject(User $user, Project $project)
{
    return $project->members->where('project_id', $project->id)
        ->where('user_id', $user->id)
        ->where('edit_project', 1);
}

And the view:

        @can('editProject', $project)
            yes
        @endcan

EDIT:

This is in Project model:

public function members()
{
    return $this->belongsToMany(User::class, 'project_members')
        ->withTimestamps()
        ->withPivot('edit_project');
}

I get 'yes' no matter what. To me, that means I'm doing something wrong in the policy. What do you think?

ajsmith_codes's avatar

It's coming back empty:

Illuminate\Database\Eloquent\Collection {#1421 ▼ #items: [] }

ajsmith_codes's avatar

I finally figured it out, but it's not very pretty in my opinion.

Changed my ProjectPolicy entry to this:

public static function editProject(User $user, Project $project)
{

        $canEdit = $project->members($user)->pluck('edit_project');

        if ($canEdit->contains(1))
            return true;
        else
            return false;

}
bugsysha's avatar

First: Don't make it static.

Second: You will figure it out how to write it better once you get to know policies well.

ajsmith_codes's avatar

What do you mean by "Don't make it static"? I'm not very familiar with that.

bugsysha's avatar
public static function editProject

Should not have static keyword in it.

Please or to participate in this conversation.