2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel CRUD処理を使った投稿アプリを作成する その2 投稿入力画面の作成

Last updated at Posted at 2020-07-16

目的

  • アプリを作成する上で基本となるCRUD処理を有したLaravelアプリをチュートリアル的に作成する方法をまとめる

実施環境

  • ハードウェア環境
項目 情報
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
  • DockerやAWSなどは使用せずにMacのローカルに実施環境と同じLaravel開発環境を構築して実施する。
  • チュートリアルで実際に筆者が作成したソースコードをGitHubにて公開予定である。
  • CRUD処理の作成完了を最短目標にしてバリデーションなどは後々設定することとする。
  • 実施環境と同じ環境がDockerやAWSで用意できるなら都度読み替えていただければ実施が可能だと思う。
  • 公式ドキュメントと一冊の技術書を元に本記事を記載する。

この記事の読後感

  • 投稿内容を入力する画面の実装ができる。

全ての記事(miriwo_laravelチュートリアル)を通した読後感

  • Laravelアプリでログインなどのユーザ認証付き投稿アプリの作成ができる。

概要

  1. ルーティングの記載
  2. コントローラの作成と記載
  3. ビューファイルの作成と記載
  4. 確認

詳細

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

      $ vi routes/web.php 
      
    2. 開いたファイルに下記の行を追記する。

      laravel_crud/routes/web.php
      Route::get('/input', 'ContentController@input')->name('input');
      
    3. 追記後のルーティングファイルの内容を下記に記載する。

      laravel_crud/routes/web.php
      <?php
      
      use Illuminate\Support\Facades\Route;
      
      /*
      |--------------------------------------------------------------------------
      | Web Routes
      |--------------------------------------------------------------------------
      |
      | Here is where you can register web routes for your application. These
      | routes are loaded by the RouteServiceProvider within a group which
      | contains the "web" middleware group. Now create something great!
      |
      */
      
      Route::get('/', function () {
          return view('welcome');
      });
      
      Auth::routes();
      
      Route::get('/home', 'HomeController@index')->name('home');
      
      Route::get('/input', 'ContentController@input')->name('input');
      
  2. コントローラの作成と記載
    1. laravel_crudディレクトリで下記コマンドを実行してコントローラファイルを作成する。

      $ php artisan make:controller ContentController
      
    2. laravel_crudディレクトリで下記コマンドを実行してコントローラファイルを開く。

      $ vi app/Http/Controllers/ContentController.php
      
    3. 開いたコントローラファイルに下記の内容を追記する。

      app/Http/Controllers/ContentController.php
      public function input()
      {
          return view('contents.input');
      }
      
    4. 追記後のコントローラファイルの内容を下記に記載する。

      app/Http/Controllers/ContentController.php
      <?php
      
      namespace App\Http\Controllers;
      
      use Illuminate\Http\Request;
      
      class ContentController extends Controller
      {
          public function input()
          {
              return view('contents.input');
          }
      }
      
  3. ビューファイルの作成と記載
    1. laravel_crudディレクトリで下記コマンドを実行してビューファイルを格納するディレクトリを作成する。

      mkdir resources/views/contents
      
    2. laravel_crudディレクトリで下記コマンドを実行してビューファイルを作成する。

      vi resources/views/contents/input.blade.php
      
    3. 作成して開いたビューファイルに下記の内容を追記する。

      laravel_crud/resources/views/contents/input.blade.php
      <h1>input</h1>
      
      <form action="" method="post">
          @csrf
          <textarea name="content" cols="30" rows="10"></textarea>
          <input type="submit" value="送信">
      </form>
      
  4. 確認
    1. laravel_crudディレクトリで下記コマンドを実行してローカルサーバを起動する。

      $ php artisan serve
      
    2. ブラウザで下記にアクセスする。

    3. 下記の様に表示されれば本記事の作業は完了である。

      127_0_0_1_8000_input.png
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?