我必须这样创建一个接口:
<?php
namespace App\Contracts;
interface EntityInterface {
public function getEntities();
public function getEntitiesWithKeyneeds();
}
我的整个历史都是这样的:
<?php
namespace App\Http\Controllers\BackOffice\Repository;
use App\Contracts\EntityInterface;
use App\Segment;
use App\Valuechain;
class EntityRepository implements EntityInterface
{
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()
{
// liste des cdv
$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;
}
}
在APP/提供者/AppService PvaveR.PHP中,我必须绑定接口。我想我必须从使用Lista\\Buffel\\ ServiceProvider重写构造函数:
public function __construct($app)
{
$this->app = $app;
$this->app->bind(
'App\Contracts\EntityInterface',
'App\Http\Controllers\BackOffice\Repository\EntityRepository'
);
}
我使用这些值的控制器如下所示(这是我的索引方法):
public function index(EntityInterface $vcs)
{
$entitiesLists = $vcs->getEntities();
$entitiesWithKnLists = $vcs->getEntitiesWithKeyneeds();
return view('admin.home', compact('entitiesLists', 'entitiesWithKnLists'));
}