Handle model dependencies in Laravel Repository Pattern

Viewed 159

I'm discovering the Repository Pattern for my Laravel project but I have to say that I'm a bit lost once a model has several dependencies and the examples on the web are always basic and don't answer the question for more complex use cases.

Let's imagine a user on my app. He can have badges, he has different things on the app that will be slightly modified when he first performs the action (when he first sees the comments, I tell him once the different things he can do, etc), he has several "counters" to record the number of comments he made, the number of friends he invited, without having to count each entry each time.

My database looks like this:

users table:

id
pseudo
name
password
...

badges table:

user_id
badge1_xxxxxx
badge2_xxxxxx
...

I have a very limited number of badges so I decided to create a column for each of them and as soon as a user wins a badge, I get his entry (in OneToOne relationship) and I indicate that the badge in question has been won.

onboarding table:

user_id
seen_comments (boolean)
seen_results (boolean)
...

As you can see, I store each action I'd like the user to do in different columns and as soon as he has done one and I've been able to modify my app accordingly (by showing him an arrow, etc), I put the column in question to true.

user_counters table:

user_id
count_comments
count_invited_friends
...

I don't consider a user to be a user if he doesn't have an entry in each of the tables (I could have done everything in one table but the users table seemed to me to become huge). The only relationship used is OneToOne between the user and the table in question.

Should I do this ?

class UserRepository {

public function register($data) {
    // Create the user
    $user = User::create($data);
    
    // Create all its dependencies which are required if I want to consider the user as fully registered in my DB
    $user->badges()->create();
    $user->onboarding()->create();
    $user->counter()->create();
    
    // Return the user
    return $user;
}

}

Or should I create a Repository for each of these elements and create the entire user in a UserService ?

How far should I separate things and when does it become overkill?

Is there something that I don't understand in concept of Repository ? If so, could you give me some links that you found useful because I feel like I ran out of ideas for search keywords.

Thanks

0 Answers
Related