5
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 post送信を解説

Last updated at Posted at 2021-06-06
mojikyo45_640-2.gif mojikyo45_640-2.gif

ルーティング

<?php
Route::get('/request_form', 'SampleController@requestForm');

Route::post('/request_sample', 'SampleController@requestSample');

コントローラー

<?php
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
class SampleController extends Controller
{
    public function requestForm(){
      return view('samples.request_form', [
        'title' => 'フォームサンプル',
      ]);
    }
    public function requestSample(Request $request){
      
      $user_name = $request->input('user_name');
      $comment = $request->input('comment');
      
      return view('samples.request_sample', [
        'title' => 'リクエストの受け取り',
        'user_name' => $user_name,
        'comment' => $comment,
      ]);
    }
}

送信フォームのビュー

<?php
@extends('layouts.default')
 
@section('title', $title)
 
@section('content')
<h1>{{ $title }}</h1>
<form method="post" action="{{ url('/request_sample') }}">
  {{-- 偽のフォームでないことを証明するためのcsrfトークンを埋め込み --}}
  @csrf
  <div>
    <label>
      名前:
      <input type="text" name="user_name">
    </label>
  </div>
  <div>
    <label>
      コメント:
      <input type="text" name="comment">
    </label>
  </div>
  <input type="submit" value="送信">
</form>
@endsection

送信先のビュー

<?php
@extends('layouts.default')
 
@section('title', $title)
 
@section('content')

<h1>{{ $title }}</h1>
<p>名前: {{ $user_name }}</p>
<p>コメント: {{ $comment }}

@endsection

共通レイアウトファイル

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>@yield('title')</title>
    <style>
        .header_nav {
            display: flex;
            list-style: none;
            padding-left: 0;
        }
        .header_nav li {
            margin-right: 30px;
        }
    </style>
</head>
<body>
    @yield('header')
    @yield('content')
</body>
</html>
5
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
5
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?