SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value というエラーが出て困っております
Q&A
解決したいこと
(レシピの)フォームから投稿できるようにしたいです。
該当のソースコードがわからないので、いくつか載せておきます。
laravelで作成中のウェブアプリケーションで、レシピを登録できるフォームを押すと、データベースに記録されるのですが、投稿ボタンを押すと、下記のエラーが出ます。
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value
発生している問題・エラー
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `user_recipe` (`name`, `url`, `description`, `updated_at`, `created_at`) values (キムチチャーハン, https://yaoply.com/items/sqlstatehy000-general-error-1364-field, な, 2022-01-13 19:52:18, 2022-01-13 19:52:18))
ControllasディレクトリのCreateRecipeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
/** 必要なモデルをuseする */
use App\Models\Recipe;
use App\Models\User;
class CreateRecipeController extends Controller
{
//バリデーションのルール
protected $validationRules = [
"name" => ["required", "string"],
"url" => ["nullable", "url"],
"description" => ["nullable", "string"]
];
function __construct(){
$this->middleware('auth');
}
//レシピ登録フォームを表示
function create(){
return view("recipe.recipe_create_form");
}
//レシピ登録フォームからの遷移先
function store(Request $request){
//入力値の受け取り
$validatedData = $request->validate($this->validationRules);
//作成するユーザーIDを設定
$validatedDate["user_id"] = \Auth::id();
//レシピの保存
$new = Recipe::create($validatedData);
//登録後は登録フォームを表示
return redirect()->route("create_recipe")
->withStatus("レシピ: {$new->name}を作成しました");
}
}
migrationsディレクトリの2022_01_10_164545_create_recipe_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRecipeTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_recipe', function (Blueprint $table) {
$table->id();
//レシピ名
$table->string('name');
//URL
$table->string('url')->nullable();
//説明
$table->text('description')->nullable();
//user_id: usersとの連携用
$table->foreignId('user_id')->constrained("users");
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_recipe');
}
}
resources/views/recipe/recipe_create_form.blade
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
レシピ登録
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
@if (session('status'))
<div class="success mt-5 px-4 text-green-900">
{{ session('status') }}
</div>
@endif
<!-- ページ固有要素 -->
<div class="mt-5 px-4 py-5">
<!-- エラー表示 -->
<form method="post" action="{{ route('store_recipe') }}">
@csrf
<div class="mb-4">
<label for="recipe_name" class="block mb-2">レシピ名</label>
<input type="text"
id="recipe_name"
class="form-input w-full"
name="name"
value="{{ old('name') }}"
placeholder="レシピ名" />
@if ($errors->has('name'))
<span class="error mb-4 text-red-900">{{ $errors->first('name') }}</span>
@endif
</div>
<div class="mb-4">
<label for="recipe_url" class="block mb-2">URL</label>
<input type="text"
id="recipe_url"
class="form-input w-full"
name="url"
value="{{ old('url') }}"
placeholder="https://xxxxx" />
@if ($errors->has('url'))
<span class="error mb-4 text-red-900">{{ $errors->first('url') }}</span>
@endif
</div>
<div class="mb-4">
<label for="recipe_description" class="block mb-2">説明</label>
<textarea type="text"
id="recipe_description"
class="form-input w-full"
name="description"
rows="5"
>{{ old('description') }}</textarea>
@if ($errors->has('description'))
<span class="error mb-4 text-red-900">{{ $errors->first('description') }}</span>
@endif
</div>
<div class="mb-4 flex items-center">
<input type="submit" value="作成" class="bg-blue-500 text-white font-bold py-2 px-4 rounded" />
</div>
</form>
</div>
<!-- /ページ固有要素 ここまで -->
</div>
</div>
</div>
</x-app-layout>
自分で試したこと
・envファイルとconfig内のdatabaseの値に相違がないかを調べる。
・自分と同じような問題で悩んでいた人の質問を見る。
0 likes