我创建了两个方法,这些方法使我想共享给后台中所有视图的sql请求:
public function getEntities()
{
$vcs = Valuechain::select('valuechains.id', 'lang_valuechain.vcname', 'lang_valuechain.vcshortname')
->withCount('segments')
->join('sectors', 'valuechains.sector_id', '=', 'sectors.id')
->join('lang_valuechain', 'valuechains.id', '=', 'lang_valuechain.valuechain_id')
->join('langs', 'lang_valuechain.lang_id', '=', 'langs.id')
->where('langs.isMainlanguage', '=', '1')
->whereNull('valuechains.deleted_at')
->whereNull('sectors.deleted_at')
->get();
return $vcs;
}
public function getEntitiesWithKeyneeds()
{
$valuechains = Valuechain::orderBy('valuechains.id')
->join('lang_valuechain', 'valuechains.id', '=', 'lang_valuechain.valuechain_id')
->join('langs', 'lang_valuechain.lang_id', '=', 'langs.id')
->where('langs.isMainlanguage', '=', '1')
->with('segments')
->with('keyneeds')
->withCount('segments')
->withCount('keyneeds')
->get();
foreach ($valuechains as $valuechain) {
$ids[] = $valuechain->id;
}
foreach ($ids as $id) {
$vcskns[] = Segment::select(
'lang_valuechain.vcname', 'lang_valuechain.vcshortname',
'lang_segment.segname', 'lang_segment.segshortname', 'segments.id',
'lang_segment.created_at', 'lang_segment.updated_at', 'lang_segment.deleted_at'
)
->distinct()
->withCount('keyneeds')
->join('lang_segment', 'segments.id', '=', 'lang_segment.segment_id')
->join('valuechains', 'segments.valuechain_id', '=', 'valuechains.id')
->join('lang_valuechain', 'valuechains.id', '=', 'lang_valuechain.valuechain_id')
->join('sectors', 'valuechains.sector_id', '=', 'sectors.id')
->join('lang_sector', 'sectors.id', '=', 'lang_sector.sector_id')
->join('langs', 'lang_valuechain.lang_id', '=', 'langs.id')
->where([
['langs.isMainlanguage', '=', '1'],
['valuechains.id', '=', $id]
])
->whereNull('valuechains.deleted_at')
->whereNull('sectors.deleted_at')
->whereNull('segments.deleted_at')
->get();
}
return $vcskns;
}
我在另一个返回视图的方法中调用此方法:
public function index(EntityInterface $vcs)
{
$entitiesLists = $vcs->getEntities();
$entitiesWithKnLists = $vcs->getEntitiesWithKeyneeds();
return view('admin.home', compact('entitiesLists', 'entitiesWithKnLists'));
}
我有一个var_dump,它为entityWithKnLists返回:
[
{
"vcname":"VC1",
"vcshortname":"VC1",
"segname":"S1",
"segshortname":"S1",
"id":1,
"created_at":"2018-06-07 09:18:31",
"updated_at":"2018-06-07 09:18:31",
"deleted_at":null,
"keyneeds_count":0
},
{
"vcname":"VC1",
"vcshortname":"VC1",
"segname":"S2",
"segshortname":"S2",
...
},
{
"vcname":"VC2",
"vcshortname":"VC2",
"segname":"S3",
"segshortname":"S3",
...
},
]
在我看来vcname是重复的,我只想显示一次,所以我在我的看法中写了以下代码:
@foreach($entitiesWithKnLists as $entitiesWithKnList)
{{ $entitiesWithKnList->first()->vcname }}<br>
@endforeach
它返回我期望的正确值。
当我想显示“ segname”时,就像在var_dump中看到的那样,我只写了类似的内容:
@foreach($entitiesWithKnLists as $entitiesWithKnList)
{{ $entitiesWithKnList->segname }}
@endforeach
我收到一条错误消息:
此集合实例上不存在属性[segname]。
谢谢
答案 0 :(得分:0)
我终于用这种方式解决了这个问题:
figcaption {
position:relative;
left: 50%;
transform: translateX(-50%);
width: auto;
}