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

CodeFox's avatar

Generate assignable links

Hey

I'd like some feedback on how to structure something. I'm currently building a quiz and want to be able to do the following:

General Quiz (so that everyone who can access that quiz can complete it as many times as they'd like)

Assigned Quiz (so that you can assign a user to complete the quiz as a one off, but they can also do the same quiz as a general quiz)

So right now I have the General Quiz code working perfectly, no problems there.

I also have code to assign a quiz, but what would be the best way to ensure that they can only complete it once, and only the user who has been assigned the quiz can access it? So far I have a pivot table of:

quiz_id
user_id

So the user can access a quiz that has been assigned to them, but what's the best method to ensure a one-off completion?

I know this is really simple and I can think of some straightforward ways to do this, but I want the code to be clean.

Thanks

0 likes
4 replies
topvillas's avatar
Level 46

Put a completed boolean in the pivot table and access it like this.

I think this will work™

$user->quizes()->find($id)->pivot->completed;
1 like
CodeFox's avatar

That was one of the solutions I was considering.

Is that clean enough though? I normally avoid putting other columns into a pivot :/

topvillas's avatar

Only you can decide it it's clean enough.

But it looks pretty damn clean to me. The table handles the relationship so why not put relationship specific data in there?

1 like
CodeFox's avatar

Fair point, I guess I'm overthinking it :)

Thanks for the help!

Please or to participate in this conversation.