laravel - 无法使用引导模型检索整个用户集合及其关系,我只收到单个用户及其关系
问题描述
我试图通过模型从数据库中检索用户详细信息,因此当我单击特定用户时,它会显示指定的用户以及所需的其他一些详细信息
我只使用引导模型,我不知道它是如何工作的,而且我对带有 jquery 的 javascript 了解不多,我只需要这个模型来完成所有事情
/* here is my blade */
@extends('inherit.admin')
@section('info')
@if(count($applieds)>0)
<div class="container">
<table class="table table-hover membertable" style="margin-top: 100px;">
<tr>
<th>Name</th>
<th>Email</th>
<th>Role</th>
<th>Status</th>
<th> Forms</th>
<th>Actions</th>
<th>View Applications</th>
</tr>
@foreach($applieds as $user)
<tr>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->role->name}}</td>
<td> @foreach ($user->post as $key=>$form)
@if($key == count($user->post)-1)
<div class="text-muted" style="float: left;padding-right: 10px">
<?php echo $form->pivot->status ?>
</div>
@endif
@endforeach
</td>
<td>
@foreach ($user->post as $key=>$form)
@if($key == count($user->post)-1)
<a href="/downloads/{{$user->id}}"> <button class="btn btn-success" style="width: 100px" ><i class="fa fa-download"></i> Download form <?php echo $form->pivot->file ?></button></a>
@endif
@endforeach
</td>
<td><a href="/approve/{{$user->id}}"> <button class="btn btn-success">Approve</button></a> <a href="/reject/{{$user->id}}"> <button class="btn btn-danger">Reject</button></a></td>
<td>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#applications">View applications</button>
<!-- Modal -->
<div class="modal fade" id="applications" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
{{-- <button type="button" class="close" data-dismiss="modal">×</button> --}}
<h4 class="modal-title">Applications</h4>
</div>
<div class="modal-body">
<div class="text-muted" style="float: left;padding-right: 10px">
@foreach($user->post as $form)
{{$form->pivot->company_name}}
@endforeach
{{$user->name}}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</td>
</tr>
@endforeach
</table>
</div>
@endif
@endsection
/* my controller */
public function index()
{
$applieds=User::where('role_id',4)->with('post')->get();
return view('Applieds.index',compact('applieds'));
}
没有错误,它只显示第一个用户的唯一两个详细信息,当我需要检索它时它只会重复它
解决方案
您可以尝试使用以下代码: data-target="#applications_{{$user->id}}" 和 id="applications_{{$user->id}}"
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#applications_{{$user->id}}">View applications</button>
<!-- Modal -->
<div class="modal fade" id="applications_{{$user->id}}" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
{{-- <button type="button" class="close" data-dismiss="modal">×</button> --}}
<h4 class="modal-title">Applications</h4>
</div>
<div class="modal-body">
<div class="text-muted" style="float: left;padding-right: 10px">
@foreach($user->post as $form)
{{$form->pivot->company_name}}
@endforeach
{{$user->name}}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
推荐阅读
- r - ggplotly() 不在 x 轴上放置日期
- android - 如何获取房间数据库的版本号?
- kubernetes - stackdriver-metadata-agent-cluster-level 很多失败与 [2a00:1450:400c:c09::5f]:443: i/o timeout
- javascript - Bootstrap-4:将面包屑与标题对齐
- c++ - 为什么在 C++ 20 中从标准库容器中删除了比较运算符?
- python - pathlib 的 `glob` 方法在运行之间的顺序是否一致?
- python - 使用 Matplotlib,如何显示以 HH24:MI 格式排序的 Y 轴值
- python - 如何正确设置包?
- symfony - Symfony 5 没有教义
- javascript - 如何通过多次单击按钮来解决 React.js 状态?