流れをまとめました。私にしか役に立たないとおも🐱
もくじ
関連
Controller
<?php namespace App\Http\Controllers\Member; use Illuminate\Http\Request; use App\Services\GetMembersScoreListService; class ScoreListController extends BaseController { protected $memberInfo; public function __construct() { parent::__construct(); $this->memberInfo = (object) session('member'); } /** * スコアリスト表示 * * @return View */ public function index( Request $request, GetMembersScoreListService $getMembersScoreListService ) { $membersScores = $getMembersScoreListService->execute($request); $requestParams = $request->all(); $departs = Depart::sortNewYear()->isNotDeleted()->get(); $hintList = (object) [ 'members' => Members::isNotDeleted()->isNotRetiredEmployee()->get(), 'departs' => $departs, ]; return view('home.status.performanceList', compact( 'membersScores', 'requestParams' )); } }
Service
<?php namespace App\Services; use App\Services\Service; use Illuminate\Http\Request; use App\Repositories\Status\Member\ScoreListRepositoryInterface; class GetUsersScoreListService { protected $scoreListRepository; public function __construct(PerformanceListRepositoryInterface $scoreListRepository) { $this->scoreListRepository = $scoreListRepository; } /** * スコアデータを返却 * * @return array(object(stdClass)) */ public function execute($request) { return $this->scoreListRepository->execute($request); } }
Interface
<?php namespace App\Repositories\Status\Member; use Illuminate\Http\Request; interface ScoreListRepositoryInterface { public function execute($request); }
Repository
<?php namespace App\Repositories\Status\Member; use App\Http\Models\Members; use App\Repositories\Status\Member\ScoreListRepositoryInterface; use Illuminate\Support\Facades\DB; class ScoreListRepository implements ScoreListRepositoryInterface { const DEFAULT_LIST_PAGINATE_AMOUNT = 20; const FIRST_INDEX = 1; public function execute($request = null) { return self::getMembersWithDepartment($request); } /** * スコアデータ返却 * * @return Illuminate\Pagination\LengthAwarePaginator */ private function getMembersWithDepartment($request) { $sectionNames = array(); $memberReps = array(); $memberRoles = array(); $sectionNames = array(); $pagesize = self::DEFAULT_LIST_PAGINATE_AMOUNT; $query = Members::query()->where(function ($query) { return $query->where('tb_member.deleted', '=', '0'); }); if (!isset($request)) { return 'データが不正です。<br/><a href="/">ホームへ戻る</a>'; } $requestParams = $request->query(); if (isset($requestParams)) { $sectionNames = $requestParams['section_name']; $memberReps = $requestParams['reps']; $memberRoles = $requestParams['roles']; $pagesize = $requestParams['pagesize']; if (isset($requestParams['reps'][self::FIRST_INDEX])) { $query->filterFullnames($memberReps); } if (isset($requestParams['roles'][self::FIRST_INDEX])) { $query->filterRoles($memberRoles); } } $subQuery = DB::table('tb_member_section') ->select('member_id', 'section_id', 'created') ->where('deleted', '=', '0') ->whereRaw('created = ( SELECT MAX(created) FROM tb_member_section AS tmp_tb_member_section WHERE tb_member_section.member_id = tmp_tb_member_section.member_id AND tb_member_section.deleted = 0 ) '); // メインクエリ実行 return $query->select( 'tb_member.member_id', 'tb_member.fullname', 'tb_member.score', 'tb_section.section_name' )->join(DB::raw('('. $subQuery->toSql() .') as ud'), function ($join) { $join->on('tb_member.member_id','=','ud.member_id'); }) ->mergeBindings($subQuery) ->leftjoin('tb_section', function ($join) { $join->on('tb_section.section_id', '=', 'ud.section_id') ->where('tb_section.deleted', '=', '0'); }) ->when(isset($sectionNames[self::FIRST_INDEX]), function($query) use ($sectionNames) { return $query->whereIn('section_name', $sectionNames); }) ->groupBy('ud.member_id') ->paginate($pagesize); } }
サービスプロバイダ
app/Providers/RepositoryServiceProvider.php
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class RepositoryServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { // } /** * Register the application services. * * @return void */ public function register() { $this->app->bind( \App\Repositories\Status\Member\ScoreListRepositoryInterface::class, \App\Repositories\Status\Member\ScoreListRepository::class ); } }
app/Providers/ScoreServiceProvider.php
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class ScoreServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { // } /** * Register the application services. * * @return void */ public function register() { $this->app->bind( \App\Services\GetUsersScoreListService::class, function ($app) { return new \App\Services\GetUsersScoreListService( $app->make('\App\Repositories\Status\Member\ScoreListRepositoryInterface') ); } ); } }
サービスとリポジトリを登録します。
config/app.php
'providers' => [ /* * Laravel Framework Service Providers... */ Illuminate\Auth\AuthServiceProvider::class, Illuminate\Broadcasting\BroadcastServiceProvider::class, Illuminate\Bus\BusServiceProvider::class, Illuminate\Cache\CacheServiceProvider::class, Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class, Illuminate\Cookie\CookieServiceProvider::class, Illuminate\Database\DatabaseServiceProvider::class, Illuminate\Encryption\EncryptionServiceProvider::class, Illuminate\Filesystem\FilesystemServiceProvider::class, Illuminate\Foundation\Providers\FoundationServiceProvider::class, Illuminate\Hashing\HashServiceProvider::class, Illuminate\Mail\MailServiceProvider::class, Illuminate\Pagination\PaginationServiceProvider::class, Illuminate\Pipeline\PipelineServiceProvider::class, Illuminate\Queue\QueueServiceProvider::class, Illuminate\Redis\RedisServiceProvider::class, Illuminate\Auth\Passwords\PasswordResetServiceProvider::class, Illuminate\Session\SessionServiceProvider::class, Illuminate\Translation\TranslationServiceProvider::class, Illuminate\Validation\ValidationServiceProvider::class, Illuminate\View\ViewServiceProvider::class, /* * Application Service Providers... */ App\Providers\RepositoryServiceProvider::class, ←●追加 App\Providers\ScoreServiceProvider::class, ←●追加
クラス再読み込み
$ composer dumpa
View ページネーション
<div class="list-paginate"> <div class="pages horizontal_pagenate"> <div>{{ $membersScores->total() }}件中 {{ $membersScores->firstItem() }}〜{{ $membersScores->lastItem() }}件</div> {{ $membersScores->appends($requestParams)->links() }} </div> </div> <div id="score_table"> <table cellspacing="0" cellpadding="0" class="table_box sortable"> <thead> <tr> <th>メンバー名</th> <th>部署</th> <th>スコア</th> </tr> </thead> <tbody> @foreach ($usersScores as $usersScore) <tr> <td>{{ $usersScore->fullname }}</td> <td>{{ $usersScore->section }}円</td> <td>{{ $usersScore->score }}円</td> </tr> @endforeach </tbody> </table> </div>
{{ $membersScores->appends($requestParams)->links() }}
appendsを利用することでパラメータを付与できます。