プログラミング初心者です。投稿機能を実装しております。
初めての質問のため、情報が不完全かもしれませんが、ご容赦ください。
migrationファイル
public function up() { Schema::create('scenes', function (Blueprint $table) { $table->id(); $table->foreignId('user_id')->constrained('users'); $table->string('title', 80); $table->string('content', 200); $table->timestamps(); }); }
Controller.php
public function store(Scene $scene, SceneRequest $request) { $this->authorize('create', Scene::class); //$this->authorize('view', $scene); $input = $request['scene']; $scene->fill($input)->save(); return redirect('/');
実現したいこと
userテーブルにあるid を外部キーとして、sceneテーブルにuser_idとして保存したい
- [ ]
前提
投稿機能を実装しようとしています。
外部キーとしてuser_id を設定した際に保存ができなくなりました。
発生している問題・エラーメッセージ
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value INSERT INTO `scenes` (`title`, `content`, `updated_at`, `created_at`) VALUES ( title1, body1, 2023 -12 -27 18: 11: 22, 2023 -12 -27 18: 11: 22 )
調査したこと・試したこと
modelファイルに親子関係を提示したが、まったく違うかもしれない
Userモデル
protected $fillable = [ 'name', 'email', 'password', ]; public function scene() { return $this->hasMany(Scene::class); }
Sceneモデル
public function user() { return $this->belongsTo(User::class); } protected $fillable = [ 'user_id', 'title', 'content', ];
補足情報(FW/ツールのバージョンなど)
どのファイルを提示すればよいのかわからなかったため、必要な情報がございましたらご教授ください。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。