Michello
Michello

Reputation: 11

How to get data from parent to child table multiple times in Laravel 8

I've got 3 tables and models: City, Club and Event.

Their relationship is that Event is a child of Club and Club is a child of City.

I know how to retrieve data from a child to a parent but how can I do this from a parent to a child?

My problem is that I don't know how to code this complex request. Also I don't know what to do in case that every request would be empty.

{{ $cityView->club->event->name }}

But even $cityView->club is empty.


What I've tried

I tried to declare another variable and by using it to get club's id where id = club_id(from city) and then do the same for Event since I need to take out event's data.

So here is how my method looks like inside my controller:

public function cityView($id)
{
    $cityView = City::find($id);

    $club = Club::get()->where('city_id', $id);
    $clubid = $club->id;

    $event = Event::get()->where('club_id', $clubid);

    dump($cityView);
    dump($event);

    dd($clubid);

    return view('frontend/cityView', ['cityView' => $cityView], ['club' => $club]);
}

Right now, my dump on $clubid gets me the correct attributes (whole clubs with city_id same as I wanted but events gives back 0 results).

I've added data for events, so I'm sure there are events connected to one of those clubs.

Screenshot of dumps

I don't know how to make a valid query.

Upvotes: 0

Views: 2273

Answers (2)

Anthony Aslangul
Anthony Aslangul

Reputation: 3847

Their relationship is that event is child of club and club is child of city.

So:

  • Event belongsTo Club
  • Club belongsTo City

In these models, you should have these relationships:

// Event.php

public function club()
{
    return $this->belongsTo(\App\Models\Club::class);
}
// Club.php

public function city()
{
    return $this->belongsTo(\App\Models\City::class);
}

public function events()
{
    return $this->hasMany(\App\Models\Event::class);
    // hasOne instead of hasMany if there will always be one Event. Also, the method name should me singular in this case, but this is just a naming convention
}
// City.php

public function clubs()
{
    return $this->hasMany(\App\Models\Club::class);
    // hasOne instead of hasMany if there will always be one Club. Also, the method name should me singular in this case, but this is just a naming convention
}

Here, I'm assuming that:

  • a City can have multiple Club
  • a Club can have multiple Event

That's why I used hasMany to define the parent -> children relationship.

In this scenario, you must loop through the children, since there can be many of them (use a foreach).

If this is not the case, don't use hasMany, but hasOne. As the name suggests, hasOne define a relation where, for instance, a City can only have one Club.

If you do this, you'll be able to {{ $cityView->club->event->name }}.

Upvotes: 2

Raju Ahmed
Raju Ahmed

Reputation: 354

When you use get() to fetch your data it returns you an object. That means your $club is an object of multiple collections. Hence, you cannot access $club->id directly.

Rather than that you can implement a foreach loop like this to access the id-

$cityView = City::find($id);
$club = Club::get()->where('city_id',$id);
foreach($club as $c){ //this loop will continue for each instance of the club
    $clubid = $c->id;   //this will return a specific id of a club
    $event = Event::get()->where('club_id',$clubid); //this will return all the events of that particular club
}

However I strongly suggest you to use Eloquent Model Relationship feature here. This may help you- https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-inverse.

Upvotes: 0

Related Questions