LoginSignup
0
0

More than 3 years have passed since last update.

PHP Laravel 6 おすすめ映画投稿サイト作成過程 4:投稿機能作成編

Last updated at Posted at 2020-09-23

ルーティング

登録画面にはcreateを使用します。

| GET|HEAD  | recommends/create           | recommends.create  | App\Http\Controllers\RecommendController@create                        | web          |

DBへの登録にはstoreを使用します。

| POST      | recommends                  | recommends.store   | App\Http\Controllers\RecommendController@store                         | web          |

viewファイルの作成

Screen Shot 2020-09-23 at 20.39.31.png
登録が必要な内容をビューファイルに設定しましょう。
今回は、以下の内容を登録対象とします。
※画像のアップロードに関しては、後日編集します

登録する内容
映画タイトル
映画URL
映画概要
感想

formタグで入力データをrecommends.store へ送る

@csrfを忘れないようにしましょう。※不正な動作を防ぐために記載します。詳細はGoogleで検索してください。

recommend/resources/views/recommends/create.blade.php
<form method="POST" action="{{route('recommends.store')}}">
@csrf
 <table>
   <thead>
     <tr>
       <th>タイトル</th>
       <th>映画URL</th>
       <th>概要</th>
       <th>感想</th>
     </tr>
     <tr>
       <th><input type="text" name='title'></th>
       <th><input type="URL" name='url'></th>
       <th><textarea name="description" id="" cols="30" rows="10"></textarea></th>
       <th><textarea name="Impressions" id="" cols="30" rows="10"></textarea></th>
       </tr>
  </thead>
 </table>
 <button type="submit">登録</button>
 <a href="{{route('recommends.index')}}">一覧に戻る</a>
</form>

レコードの保存処理

レコードの保存には、Modelのクリエイトメソッドを使用します。
登録後はindexページにリダイレクトさせました。

recommend/app/Http/Controllers/RecommendController.php
public function store(Request $request)
    {
        Recommend::create($request->all());
        return redirect()->route('recommends.index');
    }

一方、allで情報を取得した場合、そこには全ての情報が含まれているため、どの情報を登録するかModelで指定する必要があります。

recommend/app/Models/Recommend.php
class Recommend extends Model
{
    protected $fillable = [
      'title',
      'image_file_name',
      'image_title',
      'url',
      'description',
      'Impressions'
    ];
}
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