0

I have two different Models

App\Models\Team;

and

App\Models\Member;

Where my Team Model has this function

public function memberList()
{
    return $this->hasMany('App\Models\Member');
}

and I'm using this code to show it on my blade

 $team = Team::with('memberList')->get();
 return view('front.pages.custom-pages-index', compact('team'));

here's my columns for the tables

MemberTable

enter image description here

Team Table

enter image description here

Now I'm trying to access the compact data using this foreach

@if(count($team))
     @foreach($team as $field)
         //Name of Team
         <h1>{{$field->name}}</h1>
         //Member
          ....
     @endforeach
@endif

This is the sample content of compact data when I tried to access the {{ $team }} on blade file

 [{
"id":1,
"name":"Executive Team",
"created_at":"2020-05-26 04:38:27",
"updated_at":"2020-05-26 04:38:27",
"member_list":[{
    "id":1,
    "team_id":1,
    "position":1,
    "name":"Chris White, PH.D",
     "member_position":"President, Founder and CEO",
  ...And other member per specific team

The problem is I want to fetch on blade the members per specific teams

UPDATE

When I tried at least to access the name inside of the member_list using foreach

$field->member_list->name

I'm getting an error like this

Trying to get property 'name' of non-object

enter image description here

Tried to var_dump($field->member_list)

enter image description here

Tried this @php dd($field->member_list); @endphp

enter image description here

12
  • can you comment your foreach loop and dump your field_member variable inside of if statement before comment? Commented Jul 2, 2020 at 0:40
  • @umuttaymaz shows null but when I tried to {{$team}} member_list is not null Commented Jul 2, 2020 at 0:43
  • actually it shouldn't be written as NULL. you can add $field->member_list != NULL too inside of your if statement. !empty($field->member_list) OR !is_null($field->memberlist) OR $field->member_list != NULL Commented Jul 2, 2020 at 0:47
  • Hmm tried your suggestion, the problem is getting the same error. Commented Jul 2, 2020 at 0:49
  • Delete all of conditions and just give a condition. count($field->member_list) > 0 Commented Jul 2, 2020 at 0:57

2 Answers 2

1

Because member_list item of variable field is an array and "name" isn't a key of this array. You should use another loop for your member list inside of your main loop.

@if(count($team))
 @foreach($team as $field)
     //Name of Team
     <h1>{{$field->name}}</h1>

     //Member
     @if(!empty($field->member_list) OR !is_null($field->memberlist))
        @foreach($field->member_list as $member)
        <h2>{{$member->name}}</h2>
       @endforeach
     @endif
 @endforeach
@endif
Sign up to request clarification or add additional context in comments.

5 Comments

Followed your answer but got this error Invalid argument supplied for foreach()
Do you have any team which is not have any member?
You should check for $field->member_list is not empty or not null before foreach. Teams without any member cause this error
Tried something like this @if(!empty($field->member_list) || $field->member_list != NULL) @foreach($field->member_list as $member) ... the team name shows but the member of some team are not showing. Also either I use !empty or != NULL the members not showing but team name shows.
change $field->member_list != NULL to !is_null($field->member_list) and give it a try
0

In my Member.php (Model)

Added this

public function member()
{
    return $this->belongsTo('App\Models\Team', 'id', 'team_id');
}

In my Team.php (Model)

public function memberList()
{
    return $this->hasMany('App\Models\Member', 'team_id', 'id');
}

Controller

  $team = Team::with('memberList')->get();
  return view('front.pages.custom-pages-index', compact('team'));

Blade

  @if(count($team) > 0)
        @foreach($team as $field)
                {{$field->name}}
               @foreach($field->memberList as $member)
                  {{$member->image}}
               @endforeach
        @endforeach
 @endif

Need to map first the Team and Member models using its Primary key and Foreign Keys

And call the json column memberList

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.