Correlating db entries directly to a PHP Class

Published 1 month ago by mynameiscody07

I'm not sure how to do this or if what the title says is even the best way to handle my situation but I need some advice. I'll give a quick run down on what I'm trying to do below.

If anyone here is in a bowling league you know that their are all sorts of side games that people play with everyone else in the league including things like, high game, high series, brackets and many more. I'm working on building a system that allows to run all these side games, the current software for these things is ancient looking and not modern at all.

The idea is that users create leagues, they then can add bowlers to the league, each week they can add a new event and inside that event they can add what ever side games they will be running. When someone signs up they can attach the bowler to the games they want to participate in. With this the user can simply then add the bowlers scores in one place and every game they are in will know what to do with it.

Now with all that it brings me to my question. All of these game types have very different rules of how they work so they all need to be their own PHP class or classes to work correctly. How do I correlate entries in say a game_types table to their perspective PHP classes, or how can I correlate the GameType Eloquent Model to the different PHP classes based on a field in the database? There may be a better way to do this but I think by having the all the games in a table I can then use an event_game pivot table to save which games each event is running.

With all that being said in the future as more games are added I would also like some way to do it without necessarily running a new seed to add the new game type to the database, the system should just know. I'm guessing some sort of Service Provider is the answer, I'm just not sure how.

Hope this makes sense and it not just me rambling.

Cronix
Cronix
1 month ago (797,050 XP)

How do I correlate entries in say a game_types table to their perspective PHP classes, or how can I correlate the GameType Eloquent Model to the different PHP classes based on a field in the database?

It really sounds like you want polymorphic relationships. https://laravel.com/docs/5.7/eloquent-relationships#polymorphic-relations

mynameiscody07

Hmm, I'm not sure I represented my problem correctly. With polymorphic relationships I would be using a new table to store each type of game, and in my situation I don't think I want to do that because each game type will in reality have the same columns. They would all be exactly the same. I guess that might be alright but seemed like a better structure would be something like.

Table = game_types Columns = id, name, class where class is related to a php class.

Table = games Columns = id, game_type_id, rules, winner, etc

Does that make more sense?

mynameiscody07

I think doing something like Jeffrey does in the badges series https://laracasts.com/series/unlocking-badges-workshop/episodes/17 is really what I'm looking for.

Snapey
Snapey
1 month ago (1,044,115 XP)

Can you have a single Game class (Eloquent model) with a games table that is capable of storing all types of games.

Then extend the Game class with other classes that understand the specific rules and storage requirements of that type of Game?

mynameiscody07

@Snapey That might work as well. Going to play around with that method and the one I linked to in the video from Jeffrey and see how they go.

Please sign in or create an account to participate in this conversation.