如何显示多对多关系的结果以及独立于其ID的结果?
@forelse ( $papers as $paper )
<article class="article-feeds mb-4">
@foreach ($paper->activityTypes as $actType )
<div class="flex ml-4" id="activitiesInformation">
<div class="flex-1">
<div>
<p class="font-bold">{{ $actType->activity_type }}</p> for {{ $paper->title }}
</div>
</div>
</div>
@endforeach
</article>
@empty
<p class="text-xl font-display">You don't have any activities listed for you</p>
</div>
@endforelse
我有一个刀片文件来显示纸张的活动,但是我需要像在单独的活动提要中一样显示每个活动,而不管它们的ID是什么,因为我想按paper_id
键对它们进行分组要分开
对于此示例,我想查看
Expedita推荐座位。
发布Expedita席位论文。
要彼此分开,因为它们是不同的活动,但它们具有相同的纸张ID。
进一步,
这是activityTypes关系,我在说
/*success here.*/
public function activityTypes()
{
return $this->belongsToMany(ActivityType::class,
'paper_activities',
'paper_id',
'activity_type_id')
->withPivot('creator_id','display_name','body','status')
->withTimestamps() ;
}
答案 0 :(得分:1)
我希望我能理解你的想法。您想要的是将与activity types
相关的所有paper
和每个activity type
放在自己的卡上。
要实现这一点,可以从Controller传递一个名为$activity_types
的新变量,您将像这样填充:
//assuming you are returning it from the index method
public function index()
{
$papers = Paper::all();
$activity_types = $papers->flatMap->activityTypes;
return view('papers.index')
->with('activity_types', $activity_types);
}
您的刀片也应该像这样
@forelse ( $activity_types as $activity_type )
<article class="article-feeds mb-4">
<div class="flex ml-4" id="activitiesInformation">
<div class="flex-1">
<div>
<p class="font-bold">{{ $activity_type->activity_type }}</p> for {{ $activity_type->paper->title }}
</div>
</div>
</div>
</article>
@empty
<p class="text-xl font-display">You don't have any activities listed for you</p>
@endforelse
确保您与ActivityType模型中的Paper有关联。