Support the ongoing development of Laravel.io →
Eloquent
Last updated 2 years ago.
0

So we would have two tables in our DB represented by two models: Events and Games. In the 'events' table the colmuns would be: id, name, start_date, ... And in the 'games' table the columns would be: id, name, event_id, ... Where event_id is an foreign key to the column id in events.

And for the Laravel models:

class Event extends Eloquent {

    protected $table = 'events';

    public function games()
    {
        return $this->hasMany('Games');
    }

}

class Games extends Eloquent {}

So now, to retrieve all the games of the events with id 1, you only have to do this:

$games = Event::find(1)->games;

( http://laravel.com/docs/eloquent#one-to-many )

Last updated 2 years ago.
0

Can you explain to me why a relationship isn't made in the games model?

Saluki said:

So we would have two tables in our DB represented by two models: Events and Games. In the 'events' table the colmuns would be: id, name, start_date, ... And in the 'games' table the columns would be: id, name, event_id, ... Where event_id is an foreign key to the column id in events.

And for the Laravel models:

class Event extends Eloquent {

   protected $table = 'events';

   public function games()
   {
       return $this->hasMany('Games');
   }

}

class Games extends Eloquent {}

So now, to retrieve all the games of the events with id 1, you only have to do this:

$games = Event::find(1)->games;

( http://laravel.com/docs/eloquent#one-to-many )

Last updated 2 years ago.
0

The relation that there is between your 'events' table and 'games' table is one-to-many. This means that an event can have multiple games but games could only refer to an event...

When I have that sort of relation, I use it to fetch all the sub-rows (games) of the main-row (event). For example, fetch all the games for the event with ID 23.

But you're right... we can complete the model to retrieve the event of a game. For example: what's the event for the game with ID 145? You only have to add the following the function in your Games model:

class Games extends Eloquent {

    public function event()
    {
        return $this->belongsTo('Event');
    }

}
Last updated 2 years ago.
0

Great explanation. Well done.

Saluki said:

The relation that there is between your 'events' table and 'games' table is one-to-many. This means that an event can have multiple games but games could only refer to an event...

When I have that sort of relation, I use it to fetch all the sub-rows (games) of the main-row (event). For example, fetch all the games for the event with ID 23.

But you're right... we can complete the model to retrieve the event of a game. For example: what's the event for the game with ID 145? You only have to add the following the function in your Games model:

class Games extends Eloquent {

   public function event()
   {
       return $this->belongsTo('Event');
   }

}
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.