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

I'm not sure if this is what you are after, but it sounds like a 'Has Many Through' relationship.

http://laravel.com/docs/4.2/eloquent#relationships

If you scroll down, just before polymorphics, you should see it.

Hope that helps.

Last updated 2 years ago.
0

Not sure if ive read that right, but surely your tables should be something like this:

Citys
- id
- title
- state_id
- anything else you want
States
- id
- title
- anything else you want

A city 'belongs to' a state. A state 'has Many' cities.

Thus:

$aState = State::find(1);

foreach($aState->cities as $city)
{
    echo '<hr/>'.$city->title;
}

$aCity = City::find(1);
echo 'My state is '.$aCity->state->title;

Hope that helps. Mark as solved if it does!

Last updated 2 years ago.
0

Thank you. I had been working with relationships in Laravel earlier, but I think I must have been overthinking this. Laravel was just making it too easy !

Last updated 2 years ago.
0

matyhaty said: Hope that helps. Mark as solved if it does!

I'm trying ! And searching/googling...

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.