我试着问每个人这个问题,但没有人明白我为什么要这样做,或者为什么它不起作用,但这次我会尽我最大的努力来解释它。
我在我的网站上有一个简单的政府页面,显示3个小组在引导与更高的政府,高级部长和初级部长。在每个面板中,它都显示了分配给该政府级别的帐户列表。
这是我的government.blade.php:
@include('frontend.header')
<div class="col-md-12" style="padding-bottom:24px;">
<ul class="nav navtab nav-tabs" id="myTab" style="border-bottom:none;border-radius:1px;">
<li style="padding-right:13px;" class="active"><a style="" data-target="#higher_government_team" data-toggle="tab"><i class="fa fa-diamond" aria-hidden="true"></i> The Monarch/PM</a></li>
<li style="padding-right:13px;"><a style="" data-target="#senior_government_team" data-toggle="tab"><i class="fa fa-university"></i> Seniors</a></li>
<li style="padding-right:13px;"><a style="" data-target="#junior_government_team" data-toggle="tab"><i class="fa fa-university"></i> Juniors</a></li>
</ul>
</div>
<div class="col-md-8">
<div class="tab-content">
<div class="tab-pane active" id="higher_government_team">
<div class="panel panel-info">
<div class="panel panel-body">
higher gov here
</div>
</div>
</div>
<div class="tab-pane" id="senior_government_team">
<div class="panel panel-info">
<div class="panel panel-body">
senior gov here
</div>
</div>
</div>
<div class="tab-pane" id="junior_government_team">
<div class="panel panel-info">
<div class="panel panel-body">
@foreach ($juniorGovernment as $governmentMember)
{{ $governmentMember->user_id }}<br>
@endforeach
</div>
</div>
</div>
</div>
</div>
<div class="col-md-4">
</div>
</div>
@include('frontend.footer')下面是用于显示页面的控制器:
<?php
namespace App\Http\Controllers\Frontend\User;
use Auth;
use Cache;
use Illuminate\Http\Request;
use App\Database\Website\User\Roleplay;
use App\Database\Website\Roleplay\GovernmentRole;
use App\Database\Website\Roleplay\Life\LifeEvents;
class GovernmentController
{
public function getView()
{
$higherGovernment = Cache::remember('government.higher_government', 1, function() {
return GovernmentRole::where('government_type', 'higher_government')->first()->stats;
});
$seniorGovernment = Cache::remember('government.senior_government', 1, function() {
return GovernmentRole::where('government_type', 'senior_ministers')->first()->stats;
});
$juniorGovernment = Cache::remember('government.junior_government', 1, function() {
return GovernmentRole::where('government_type', 'junior_ministers')->first()->stats;
});
return view('frontend.community.government', compact('juniorGovernment', 'seniorGovernment', 'higherGovernment', 'royalty'));
}
}现在如您所见,我只选择了government_type匹配的第一条记录。但问题是,我在该政府类型中有多个政府角色,我希望获得该政府类型的所有记录的所有帐户统计信息。我看过其他的建议,但没有一个有效,要么抛出错误,要么不做我想做的事情。
现在,我有每个角色扮演和GovernmentRoles的模态,我已经在下面发布了它们。
GovernmentRole:
namespace App\Database\Website\Roleplay;
use Eloquent;
class GovernmentRole extends Eloquent
{
protected $primaryKey = 'id';
protected $table = 'srp_government_roles';
public $timestamps = false;
protected $fillable = [];
public function stats(){
return $this->hasMany('App\Database\Website\User\Roleplay', 'government_id');
}
}角色扮演:
use Eloquent;
class Roleplay extends Eloquent
{
protected $primaryKey = 'id';
protected $table = 'srp_user_statistics';
public $timestamps = false;
protected $fillable = [];
public function user()
{
return $this->belongsTo('App\Database\Website\User\Player', 'user_id', 'id');
}
public function government_role()
{
return $this->belongsTo('App\Database\Website\Roleplay\GovernmentRole', 'government_id');
}
}因此,我要实现的不仅仅是获取它在数据库中找到的第一个政府角色记录的所有角色扮演实例,而是将与government_type匹配的所有记录的所有角色扮演实例放在一起,无论是higher_government、senior_ministers还是junior_ministers。
发布于 2017-01-07 19:01:06
如果我明白你在问什么,你能不能不做一些事情,比如通过刀片获取循环上的统计数据?例如(我删除了缓存,它增加了问题/答案的复杂性,但没有任何好处):
// controller
$higherGovernment = GovernmentRole::where('government_type', 'higher_government')->get();
// blade
@foreach($higherGovernment as $g)
{{ $g->id }}
{{ $g->name }}
@foreach($g->stats as $stat)
{{ $stat->id }}
{{ $stat->something }}
@endforeach
@endforeachhttps://stackoverflow.com/questions/41519817
复制相似问题