Laravel 5.3 Blade foreach 循环:显示来自两个不同数据库 eloquent 查询的循环数据
Laravel 5.3 Blade foreach loop : show data in loop from two different database eloquent queries
我有两个 table。
- 内容类型
- 内容
contenttype returns 我的内容类型列表,我用 foreach 在页面上显示它们。例如快照中显示的救护车服务、血库、诊所等。
同时我正在从另一个 table(contents) 中获取每种类型的内容总数。
我成功获取了每种类型的内容总数,并使用 foreach 显示在 blade 上。
但情况是我想显示每种内容类型的内容数量。
像这样
救护车服务 8,
血库 7,
诊所 4。
我的控制器方法是:
public function index()
{
if (Gate::allows('edit-content', auth()->user())) {
// below line returns list of content type e.g Ambulance service
$type = DB::table('contenttype')->distinct('label')->orderBy('label', 'asc')->paginate(10);
//below line counts the number of each content type e.g. Ambulance service 10.
$count = Content::selectRaw('type, count(*)total')->groupBy('type')->get();
return view('admin.content_listing', compact('type', 'count'));
} else {
abort(403, "Unauthorized");
}
}
这是blade代码:
@foreach ($count as $c)
<span class="label label-danger">{{ $c->total }} </span>
@endforeach
输出这个红色数字列表:
@foreach ($type as $t)
<div class="list-group-item">
<a href="{{ route('content.type.listing', $t->type ) }}" > {{ $t->label }}
<span class=" pull-right glyphicon glyphicon-search"></span></a>
<a href="{{ route('content.add.form') }}" class="pull-right "><span class="col-md-1 glyphicon glyphicon-plus-sign"></span></a>
</div>
@endforeach
输出为:
如果我将上面的循环放在第二个循环中,那么它当然会变成我不想要的嵌套循环。
我需要显示救护车 8,
美容诊所 8,
血库 1,
等等
如果有人知道解决方案,请分享!
我尝试了不同的方法但没有成功。
您是否尝试过在单个查询中执行联接,而不是创建两个查询并尝试在视图中组合它们的结果?对您的列名做出某些假设并撇开分页,实际的 SQL 类似于:
SELECT contenttype.*, count(content.id) FROM contenttype LEFT JOIN content ON contenttype.id = content.type GROUP BY contenttype.label ORDER BY contenttype.label ASC;
使用 Laravel 的查询构建器功能实现此查询所需的代码在 documentation.
中有很好的记录
我有两个 table。
- 内容类型
- 内容
contenttype returns 我的内容类型列表,我用 foreach 在页面上显示它们。例如快照中显示的救护车服务、血库、诊所等。
同时我正在从另一个 table(contents) 中获取每种类型的内容总数。
我成功获取了每种类型的内容总数,并使用 foreach 显示在 blade 上。 但情况是我想显示每种内容类型的内容数量。 像这样 救护车服务 8, 血库 7, 诊所 4。 我的控制器方法是:
public function index()
{
if (Gate::allows('edit-content', auth()->user())) {
// below line returns list of content type e.g Ambulance service
$type = DB::table('contenttype')->distinct('label')->orderBy('label', 'asc')->paginate(10);
//below line counts the number of each content type e.g. Ambulance service 10.
$count = Content::selectRaw('type, count(*)total')->groupBy('type')->get();
return view('admin.content_listing', compact('type', 'count'));
} else {
abort(403, "Unauthorized");
}
}
这是blade代码:
@foreach ($count as $c)
<span class="label label-danger">{{ $c->total }} </span>
@endforeach
输出这个红色数字列表:
@foreach ($type as $t)
<div class="list-group-item">
<a href="{{ route('content.type.listing', $t->type ) }}" > {{ $t->label }}
<span class=" pull-right glyphicon glyphicon-search"></span></a>
<a href="{{ route('content.add.form') }}" class="pull-right "><span class="col-md-1 glyphicon glyphicon-plus-sign"></span></a>
</div>
@endforeach
输出为:
我需要显示救护车 8, 美容诊所 8, 血库 1, 等等
如果有人知道解决方案,请分享! 我尝试了不同的方法但没有成功。
您是否尝试过在单个查询中执行联接,而不是创建两个查询并尝试在视图中组合它们的结果?对您的列名做出某些假设并撇开分页,实际的 SQL 类似于:
SELECT contenttype.*, count(content.id) FROM contenttype LEFT JOIN content ON contenttype.id = content.type GROUP BY contenttype.label ORDER BY contenttype.label ASC;
使用 Laravel 的查询构建器功能实现此查询所需的代码在 documentation.
中有很好的记录