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

roulendz's avatar

[L5] Need help with Roles and Premissons

Hello forum!

I am building simple app for Courses, Lessons and Tests for each Lesson. I will explain all, so you would be able to give me best advice how to tackle this problem what I have.

App is in some way similar to Laracasts :) Laracasts have Series, I have Courses, under courses there are for each course many Lessons.

Then Lesson have one more thing, it has Tests. Each Lesson have one Test.

So Courses->Lessons->Test

The problem what I have is that:

  1. To Access Course lessons and see Lesson list, you need a permission from Admin. (Member requests access to any course)

  2. After Admin gives permission Member has access to Course->Lesson list and access to reed/see first Lesson.

  3. After studying first Lesson Member again requests access form Admin for First Lesson Test.

  4. After Member submits Test answers Admin review answers and if all ok, he again gives access for Second lesson.

And all starts from 2nd step.

Admin gives access to second Lesson, Member, request access for second Lesson Test Member submits second Lesson Test answers Admin reviews and gives access for third Lesson.

What could be the best way to handle such type of permissions? For now I do not have any idea, what could be the bast way to set up database for permissions and roles.

Big thanks in advance!

0 likes
6 replies
willvincent's avatar

You'd probably want a pivot table with attributes, or a pivot model, to represent a relationship between a user and a course. That solves the first issue of granting access to a course for a specific user. Then you'd have some middleware that checks if the current user has access to the course/lesson they're trying to access.

Then you may also want another to represent their progress. Though that could probably be managed with the same pivot table/pivot model.

I think you could manage it with a table structure for your pivot table something like this:

$table->integer('user_id');
$table->integer('course_id');
$table->integer('progress')->unsigned()->nullable();

user_id and course_id are obvious. The progress field could simply represent the highest lesson number they are able to access.

The admin, after grading a test would hit a button or something to update 'progress' to the next value, on successful pass of the test.

This would work if for each course you have lessons numbered logically 1-10, for example. So by default, when a user is granted access to a course, you could set 'progess' to 1, indicating that they have access to lesson 1, and its associated test. After they take that test, and pass, progress would be updated to 2, and so on.

Looking at your post again, I see you have some other conditions for allowing access to tests, but the same general approach would probably work out just fine I think.

roulendz's avatar

Thanks, really good approach. hmm, but one more question. Access to test for lesson is given after you submit that you are finished studying, and then Admin gives access to Lesson Test.

While Member is doing test he cannot access that Lesson material.

Crazy it is so difficult. :D

Maybe there could be lessons_progress as you told, but also test_status

0 - > No access to test

1 - > Access to test and no access to lesson

2 - > Access to test and lesson after Admin accepts Test.

bobbybouwmann's avatar

You can use middleware for that as well. You can check if the user is has finished studying and set something in the database for that. Let's say that you have a table with these fields:

id
title
description
studyingDone
testsDone

Then you can easily check if the user has finished something or not and it is easy to assign for the admin ;)

Note: This is just bad naming, I'm a bit tired :P

willvincent's avatar

Crazy it is so difficult.

Only as difficult as you make it for yourself. ;)

Just think through the steps logically. How do you need to track each piece, and how do you want to store that?

All of it could be managed with a single pivot table with attributes, and then middleware can enforce access to things outside of what they have access to as defined in that table.
The values in that pivot table can also be leveraged to determine what to show users on list pages too.

roulendz's avatar

For now I have Courses -> Lessons relationship If I make Relationship between Users and Courses

Will I be able to access User-> Lessons or User->Courses->Lessons?

Like to print out All user finished lessons.

And the same with Lessons Tests if I make such relation ship. Will I be able to print out All Course Tests?

what could be best relationships between all Models?

User->Course->Lesson->Test

Please or to participate in this conversation.