php - 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 new laravel , learning laravel 5.2 mariadb 10.0.xx.

there several tables in brief 'member' can have many 'group' , display member's first name , last name name of groups member joined in same row.

i don't have clear idea achieve trying create 2 separate queries ($memberlists , $grouplists) , group list inputting first query ($memberlists)'s primary key second query ($grouplists).

or if have better idea please let me know.

i want display below

first name / last name / groups john            doe         group, b group jane            taylor      b group nick            kay         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 view

@extends('masterlayout')  <table> <thead> <th> first name </th> <th> last name </th> <th> groups </th> </thead> <tbody>  @foreach ($memberlists $memberlist) <tr> <td> {{ memberlist->firstnm}}</td> <td> {{ memberlist->lastnm }}</td> <td> {{ grouplists->groupnm }} </td> </tr> @endforeach  </tbody> <table> 

well grate example why use eloquent models instead of queries. resolved in tho lines eager loading.

in example you'll have group first of (i'll omit joins besause don't know if need here):

controller

//i'm selecting member id (i don't know how call it) $memberlists = db::table('member')     ->select('id', 'firstnm','lastnm')     ->get();   $grouplists = db::table('group')     ->select('groupnm', 'mbrcd')     ->get();  $groups = [];  foreach($grouplists $group) {     if (!isset($group[$group->mbrcd])) {         $group[$group->mbrcd] = [];     }     $group[$group->mbrcd][] = $group->groupnm; }  return view('member.memberlist')     ->with('memberlists', $memberlists)     ->with('grouplists', $groups); 

then in view:

@extends('masterlayout')  <table> <thead> <th> first name </th> <th> last name </th> <th> groups </th> </thead> <tbody>  @foreach ($memberlists $memberlist)     <tr>         <td> {{ memberlist->firstnm}}</td>         <td> {{ memberlist->lastnm }}</td>         <td> {{ isset($groups[$memberlist->id]) ? implode(', ', $groups[$memberlist->id]) : '' }} </td>     </tr> @foreach  </tbody> <table> 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -