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

El_Matella's avatar

Relations between 3 ideas

Hi everyone,

I am building a small project about poker but I am having some problems representing something with the relations:

A hand is played by many players while a player plays a hand. So I have two tables, players and hands. A hand belongs to many players while a player has many hands. But I am also trying to associate dealt cards to my players and my hand. So for the moment I have four tables:

players
    id
    pseudo
hands
    id
cards
    id
    value
    color
dealt_cards
    id
    hand_id
    player_id
    card_id

I looked at the manytomanythrough relations on Laravel's documentation but it doesn't seem to correspond to what I am looking for..

In the best way, I would like to access to dealt cards in this way:

$hand = Hand::first();
$player = $hand->players()->first();
$collection = getDealtCards($hand,$player);

Thank you very much for your help!

0 likes
5 replies
bestmomo's avatar

Hi,

You need a many to many relation between players and hands with dealt_cards as pivot table.

El_Matella's avatar

Oups, I made a mistake in my explanation, in fact, there is a fourth database of cards, so a player can have one, two, or three cards in a hand. I edited the question so the problem is clear.. Sorry about that...

El_Matella's avatar

Merci beaucoup! The package seems to be the perfect one! I'll look at the post if I run into some issues, have a nice night!

bestmomo's avatar

The package is for Laravel 4.0 but I think you can make it work on Laravel 5.0, look at the post.

Please or to participate in this conversation.