I want to display information to the user showing what communities they are in, inside of the view. Here is the controller method where I'm getting the data:
public function index()
{
$communities = array();
$usersCommunities = CommunityMemberList::where('user_id', Auth::user()->id)->get();
foreach ($usersCommunities as $community) {
$communitiesInfo = Community::where('id', $community->community_id)->get();
array_push($communities, $communitiesInfo);
}
return view('home', compact('communities', 'badges'));
}
$userCommunities gets all of the community ids that the user has from a table. Then I want to get the all of the communities information from another table using the community_id.
So basically there are 2 tables:
community_member_listwhich has acommunity_idand auser_idcolumncommunitieswhich has irrelevant information about a community
The community_id from the first table will correspond to the id in the second table.
This works, it gets all of the communities information data that the user is in and returns this data after being pushed into the $communities array from the foreach loop:
array:2 [▼
0 => Collection {#236 ▼
#items: array:1 [▼
0 => Community {#249 ▼
#connection: "mysql"
#table: "communities"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▶]
#original: array:10 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
]
}
1 => Collection {#250 ▼
#items: array:1 [▼
0 => Community {#251 ▼
#connection: "mysql"
#table: "communities"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▶]
#original: array:10 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
]
}
]
However, if I return this to the view I cannot access the specific data values such as a community name or description. And if I try using something like array_pop or array_shift, it only returns either the last community array or the first community array.
(This is the view file for reference:)
@if(!empty($communities))
@foreach($communities as $community)
<div class="card communityCard bg-dark col-md-3">
<img class="communityIcon" src="/images/communityIcons/{{$community->icon_path}}">
<div class="communityInformation">
<span class="communityTitle">{{$community->name}}</span>
<span class="userCount"><i class="fas fa-users"></i> {{$community->user_count}}</span>
</div>
<button class="btn launchCadBtn btn-primary btn-sm">Launch CAD</button>
</div>
@endforeach
@else
<span class="subTitle">You are not in any Communities. To join a Community, press the <i class="fas fa-plus-circle"></i> button and enter a Community ID</span>
@endif
Thanks in advance ;)