mardi 24 mai 2016

Laravel 5.2 How can I display member list with his / her groups in the same row (One to Many ( or Many to Many ) data display)

I am new to Laravel and learning Laravel 5.2 with MariaDB 10.0.xx.

There are several tables but in brief 'Member' can have many 'Group' and I would like to display Member's First Name and Last Name with the name of groups that member already joined in the same row.

I don't have any clear idea to achieve this but what I am trying to do is to create two separate queries ($memberLists and $groupLists) and get the group list by inputting first query ($memberLists)'s primary key to second query ($groupLists).

or if you have better idea please let me know.

I want display as below

first Name / Last Name / Groups
john            Doe         A group, B group
Jane            Taylor      B group
Nick            Kay         A group, B group, C group

My controller

$memberLists = DB::table('Member')
                ->select('firstNm','lastNm')
                ->join('Center', 'Member.mbrCd', '=', 'Center.mbrCd')
                ->join('Org', 'Center.orgCd', '=', 'Org.orgCd');

$groupLists = DB::table('Group')
                ->select('groupNm')
                ->join('Center','Center.orgCd','=','Group.orgCd')
                ->join('GroupMember ','GroupMember.mbrCd','=','Center.mbrCd')
                ->where ('GroupMember.mbrCd',$Member_primaryKey_from_memberLists);

view return view('member.memberList')
                ->with('memberLists', $memberLists)
                ->with('groupLists',$groupLists);

This is my view

@extends('masterLayOut')

<table>
<thead>
<th> First Name </th>
<th> Last Name </th>
<th> Groups </th>
</thead>
<tbody>

@foreach ($memberLists as $memberList)
<tr>
<td> </td>
<td> </td>
<td>  </td>
</tr>
@foreach

</tbody>
<table>



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1WS1KLs
via IFTTT

Aucun commentaire:

Enregistrer un commentaire