Open1

Laravelでやらかした記録を残していく場所。

KaiKai

#SQLSTATE[HY000]: General error: 1364 Field 'XXXX' doesn't have a default value (SQL: insert into
#authors (updated_at, created_at) values (2024-04-01 13:03:09, 2024-04-01 13:03:09))

src/resources/views/layouts/default.blade.php

<!DOCTYPE html>
 <html lang="ja">

 <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>@yield('title')</title>
     <style>
         body {
             font-size: 16px;
             margin: 5px;
         }

         h1 {
             font-size: 60px;
             color: white;
             text-shadow: 1px 0 5px #289ADC;
             letter-spacing: -4px;
             margin-left: 10px
         }

         .content {
             margin: 10px;
         }
     </style>
 </head>

 <body>
     <h1>@yield('title')</h1>
     <div class="content">
         @yield('content')
     </div>
 </body>

 </html>

src/resources/views/add.blade.php

@extends('layouts.default')
<style>
    th {
        background-color: #289ADC;
        color: white;
        padding: 5px 40px;
    }

    tr:nth-child(odd) td {
        background-color: #FFFFFF;
    }

    td {
        padding: 25px 40px;
        background-color: #EEEEEE;
        text-align: center;
    }

    button {
        padding: 10px 20px;
        background-color: #289ADC;
        border: none;
        color: white;
    }
</style>
@section('title', 'add.blade.php')

@section('content')
    <form action="/add" method="post">
        <table>
            @csrf
            <tr>
                <th>name</th>
                <td><input type="text" name="name"></td>
            </tr>
            <tr>
                <th>age</th>
                <td><input type="text" name="age"></td>
            </tr>
            <tr>
                <th>nationality</th>
                <td><input type="text" name="nationality"></td>
            </tr>
            <tr>
                <th></th>
                <td><button>送信</button></td>
            </tr>
        </table>
    </form>
@endsection

src/app/Http/Controllers/AuthorController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Author;
use Illuminate\Support\Facades\Auth;

class AuthorController extends Controller
{
    //データ追加用ページの表示
    public function add()
    {
        return view('add');
    }

    //データの追加機能
    public function create(Request $request)
    {
        $form = $request->all();
        Author::create($form);
        return redirect('/');
    }

    //データの一覧ページの表示
    public function index()
    {
        $authors = Author::all();
        return view('index', ['authors' => $authors]);
    }
}

src/app/Models/Author.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Author extends Model
{
    use HasFactory;
//fillable = 書き換え可能
    protected $fillable = ['name', 'age', 'nationality'];
}

と、ここまでかけたが/addにアクセスすると

SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (SQL: insert into `authors` (`updated_at`, `created_at`) values (2024-04-01 13:03:09, 2024-04-01 13:03:09))

src/routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthorController;

/*
|--------------------------------------------------------------------------
| 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('/', [AuthorController::class, 'index']);
Route::get('/add', [AuthorController::class, 'add']);
Route::get('/add', [AuthorController::class, 'create']);

これでいけると思ったが、やはりエラーを吐いた。そこで半日考えて気づいた。web.phpには、blade.phpの方で、method="post"になっているので、createをpostで書かないと、いけないと。そこで、Route::post('/add', [AuthorController::class, 'create']);にしてみたら、Createできました。