1

I have two tables and I want to join them. I have looked at other examples and just can't figure it out.

My tables are as follows.

  • Clubs
    • id
    • name
  • Members
    • id
    • club_id
    • name

A club has many members
A member belongs to a club

I can list my clubs and after listing my clubs i can list the members of the club ok, but what I want to do is list all my members and show their club. So my output would be ID, Name, Club Name

This is my code setup so far

// clubs controller
public function index()
{
    $clubs = Club::all();
    return View::make('clubs.list')->with('clubs', $clubs);
}

public function show($id)
{
    $club = Club::findOrFail($id);
    $members = $club->Members()->get();
    return View:: make('clubs.show')->with('club', $club)->('members', $members);
}

// club model
class Club extends Eloquent
{
    // each club has many members
    public function Members()
    {
        return $this->hasMany('Member');
    }
}


// members controller
public function index()
{
    $members = Member::all();
    return View::make('members.list')->with('members', $members);
}

// member model
class Member extends Eloquent
{
    // each member belongs to one club
    public function club()
    {
        return $this->belongsTo('Club');
    }
}
3
  • What about foreach(Member::all() as $member) { echo $member->name . ' belongs to the club ' . $member->club->name; }? Commented Sep 16, 2014 at 15:13
  • ive tried that in both the controller method and on the blade page. Both work but would one be making more queries? Commented Sep 16, 2014 at 15:28
  • Right, you're making a query for the club on each iterarion. What you're actually looking for is eager loading. Member::with('club')->get() would only execute 2 queries (select * from members and select * from clubs where id in (....)) Commented Sep 16, 2014 at 15:42

1 Answer 1

3

In your Member model declare the following relationship method:

public function club()
{
    return $this->belongsTo('Club');
}

So now you can query all members with their Club info as:

$members = Member::with('club')->get();

In your View when looping the Members you may try something like this:

@foreach($members as $member)
    {{ $member->name }}
    {{ $member->club->name }}
@endforeach

This is not Eloquent Join but using it's relationship technique you may do it as given above, if you want to join then you may try something like this:

$members = Member::join('clubs', 'members.club_id', '=', 'clubs.id')
                 ->select('members.*', 'clubs.name as club_name')
                 ->get();

In this case, you may loop and use the Club info like this;

@foreach($members as $member)
    {{ $member->name }}
    {{ $member->club_name }}
@endforeach
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.