Terrabyte
Terrabyte

Reputation: 342

Octobercms/Laravel get id based on relation

In Octobercms I have a list of Shows (Primary). They have a hasMany relation to Seasons and Seasons belong to the show. Then i have episodes that belong to the season (season hasMany episodes)

In the picture below what I want to do is get the season that belongs to the show, not every season created which is what is showing below. Does anyone with laravel or octobercms knowledge know how i could implement this?

The way octobercms works is that it populates the dropdown by relations or manually via the model. Im doing it by relations:

    public $hasMany = [
        'episodes' => ['Teranode\Anime\Models\AnimeEpisode', 'key' => 'season_id'],
    ]; 

These two links would help but not what im trying to get: http://octobercms.com/docs/database/relations
http://octobercms.com/docs/backend/relations

Upvotes: 3

Views: 472

Answers (2)

Terrabyte
Terrabyte

Reputation: 342

I used Request::segement() to get the Id of the show, in the Episode model under the dropdown function:

public function getSeasonOptions()
{
    return Season::where('anime_id', Request::segment(6))->lists('title', 'id');
}

This process can help on nested relations, however the segment amount will have to be modified if you make the url of the primary page deeper.

@haakym the $show = Show::find(1); didnt really help since im trying to get the id automatically not manually. I understand that its suppose to be helpful but that example didnt help

Upvotes: 0

haakym
haakym

Reputation: 12368

What is the context of your Create Episodes modal? Surely the show should be defined if it's not already in the modal?

As I understand it your relations work like this:

Show hasMany Season

Season hasMany Episode

What I would recommend is to try populating the drop-down with the specific Show's Seasons, then you will only see the Seasons that belong to the Show.

So in code that would look like this...

Let's say we want to get all the seasons for the show with an id of 1:

$show = Show::find(1);

$seasons = $show->seasons; // seasons that belong to show where id = 1

Hope that helps?

Upvotes: 0

Related Questions