我想向用户显示信息,显示他们在视图内部的communities
。这是我获取数据的控制器方法:
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
从表中获取用户拥有的所有社区ids
。然后我想使用community_id
从另一个表中获取所有社区信息。
所以基本上有2个表:
community_member_list
有community_id
和user_id
专栏communities
,其中包含有关社区的无关信息第一张表中的community_id
将对应第二张表中的id
。
这样可以获取用户所在的所有社区信息数据,并在从$communities
循环推入foreach
数组后返回此数据:
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 [▶]
}
]
}
]
但是,如果我将其返回到视图,则无法访问特定数据值,例如社区name
或description
。如果我尝试使用像array_pop
或array_shift
这样的东西,它只返回最后一个社区数组或第一个社区数组。
(这是视图文件供参考:)
@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
提前致谢 ;)
只需在控制器中返回以下值:
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)->first();
array_push($communities, $communitiesInfo);
}
return view('home', ['communities' => $communities, 'badges' => $badges]);
}
并且视图应该正常工作。问题是你试图访问$communities
作为集合,但它实际上是一个社区阵列,由于get()和徽章列表,它本身就是集合的集合。
试试这个解决方案它只有2个查询
public function index()
{
$communitiesIds = CommunityMemberList::where('user_id', Auth::user()->id)->pluck('community_id');
$communities = Community::whereIn('id', $communitiesIds)->get();
return view('home', ['communities' => $communities, 'badges' => $badges]);
}