LoginSignup
0
0

More than 3 years have passed since last update.

Laravel CRUD処理を使った投稿アプリを作成する その11 DB問い合わせ処理をServiceに分割する 番外編

Posted at

目的

  • 作成したLaravelアプリDB問い合わせ処理部分をServiceに分割する方法をまとめる

実施環境

  • ハードウェア環境
項目 情報
OS macOS Catalina(10.15.5)
ハードウェア MacBook Pro (13-inch, 2020, Four Thunderbolt 3 ports)
プロセッサ 2 GHz クアッドコアIntel Core i5
メモリ 32 GB 3733 MHz LPDDR4
グラフィックス Intel Iris Plus Graphics 1536 MB
  • ソフトウェア環境
項目 情報 備考
PHP バージョン 7.4.3 Homwbrewを用いて導入
Laravel バージョン 7.0.8 commposerを用いてこちらの方法で導入→Mac Laravelの環境構築を行う
MySQLバージョン 8.0.19 for osx10.13 on x86_64 Homwbrewを用いてこちらの方法で導入→Mac HomebrewでMySQLをインストールする

前提条件

前提情報

  • ソースコードはこちら→https://github.com/miriwo0104/laravel_crud/tree/master
  • 本当はシリーズとして続けるつもりだったがLaravel7のサポートが終了したためやむを得ず番外編として投稿する。目標は変わらないLaravel8のチュートリアルを現在作成中である。

概要

  1. サービスファイルの作成
  2. コントローラファイルの修正
  3. 確認

詳細

  1. サービスファイルの作成

    1. laravel_crudディレクトリで下記コマンドを実行してサービスファイルを格納するディレクトリを作成する。

      $ mkdir app/Services
      
    2. laravel_crudディレクトリで下記コマンドを実行してサービスファイルを作成する。

      $ vi app/Services/ContentService.php
      
    3. 先に作成し、開いたサービスファイル下記の内容を記載する。

      laravel_crud/app/Services/ContentService.php
      <?php
      
      namespace App\Services;
      
      use App\Models\Content;
      
      class ContentService
      {
          public function getContent(){
              $contents_get_query = Content::select('*');
              return $contents_get_query->get();
          }
      
          public function getEditContent($content_id){
              $contents_edit_query = Content::select('*');
              $contents_edit_query->where('id', $content_id);
              return $contents_edit_query->get();
          }
      }
      
  2. コントローラファイルの修正

    1. laravel_crudディレクトリで下記コマンドを実行してコントローラファイルを開く。

      $ vi app/Http/Controllers/ContentController.php
      
    2. 下記の様にコントローラファイルを修正する。

      laravel_crud/app/Services/ContentService.php
      <?php
      
      namespace App\Http\Controllers;
      
      use Illuminate\Http\Request;
      use App\Models\Content;
      use App\Http\Requests\ContentRequest;
      // 下記を追記する
      use App\Services\ContentService;
      
      class ContentController extends Controller
      {
          // 下記を追記する
          private $contentService;
      
          public function __construct(ContentService $contentService)
          {
              $this->contentService = $contentService;
          }
          // 上記までを追記する
      
          public function input()
          {
              return view('contents.input');
          }
      
          public function save(ContentRequest $contentRequest)
          {
              $input_content = new Content();
              $input_content->content = $contentRequest['content'];
              $input_content->save();
      
              return redirect('/output');
          }
      
          public function output()
          {
              // 下記を修正する
              $all_contents = $this->contentService->getContent();
      
              return view('contents.output', [
                  'all_contents' => $all_contents,
              ]);
          }
      
          public function delete(Request $request)
          {
              $contents_delete_query = Content::select('*');
              $contents_delete_query->where('id', $request['content_id']);
              $contents_delete_query->delete();
      
              return redirect('/output');
          }
      
          public function edit($content_id)
          {
              $edit_contents = $this->contentService->getEditContent($content_id);
              $edit_content = $edit_contents[0];
      
              return view('contents.edit', [
                  'edit_content' => $edit_content,
              ]);
          }
      
          public function update(ContentRequest $contentRequest)
          {
              $contents_update_query = Content::select('*');
              $contents_update_query->where('id', $contentRequest['content_id']);
              $update_contents = $contents_update_query->get();
      
              $update_content = $update_contents[0];
              $update_content->content = $contentRequest['content'];
              $update_content->save();
      
              return redirect('/output');
          }
      }
      
  3. 確認

    1. laravel_crudディレクトリで下記コマンドを実行してローカルサーバを起動する。

      $ php artisan serve
      
    2. ユーザ認証後、下記にアクセスして正常にページが表示されることを確認する。(投稿編集ページはidが1以外のものでも良い)

    3. なんの変化もなく表示されていれば作業完了である。

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0