独学でLaravelを勉強している者です。
まだまだ知識が浅く、見当外れなことをしているかもしれません。
ご多忙の中大変恐縮ですが、何卒宜しくお願い致します。
###実現したいこと
実現したいこととしては、質問に対して複数の回答を登録できる形式のアプリケーションを作っています。
具体的には、ユーザーが作りたい料理をオーダーして、
第三者がそのオーダーに沿ったレシピを投稿するという内容です。
質問に対して複数の回答を登録できるようにControllerで保存したいのですが、
どのようにしたら宜しいでしょうか。
どなたかおわかりになる方がおられましたらご教示ください。
発生している問題・エラーメッセージ
SQLSTATE[HY000]: General error: 1364 Field 'order_id' doesn't have a default value (SQL: insert into recipes
(ingredient
, title
, portion
, how_to_1
, how_to_2
, how_to_3
, how_to_4
, how_to_5
, how_to_6
, how_to_7
, how_to_8
, how_to_9
, how_to_10
, how_to_11
, how_to_12
, one_point
, user_id
, updated_at
, created_at
)
Migration(Recipe)
class CreateRecipeTable extends Migration
{
public function up() { Schema::create('recipes', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned()->index(); $table->integer('order_id')->unsigned()->index(); $table->string('ingredient'); $table->string('title'); $table->string('portion'); $table->string('how_to_1'); $table->string('how_to_2')->nullable(); $table->string('how_to_3')->nullable(); $table->string('how_to_4')->nullable(); $table->string('how_to_5')->nullable(); $table->string('how_to_6')->nullable(); $table->string('how_to_7')->nullable(); $table->string('how_to_8')->nullable(); $table->string('how_to_9')->nullable(); $table->string('how_to_10')->nullable(); $table->string('how_to_11')->nullable(); $table->string('how_to_12')->nullable(); $table->string('one_point')->nullable(); $table->timestamps(); $table->foreign('user_id')->references('id')->on('users'); $table->foreign('order_id')->references('id')->on('orders'); }); } public function down() { Schema::dropIfExists('recipes'); }
}
Migration(Order)
class CreateOrderTable extends Migration
{
public function up() { Schema::create('orders', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned()->index(); $table->string('ingredient'); $table->string('capability'); $table->string('outline'); $table->string('details'); $table->timestamps(); $table->foreign('user_id')->references('id')->on('users'); }); } public function down() { Schema::dropIfExists('orders'); }
}
Controller
$request
:回答内容が格納されています
$id
:回答したい "order(=質問)" の id が格納されています
recipes()
:return $this->hasMany(Recipe::class);
を返してます
public function store(Request $request,$id){
$request->user()->recipes()->create([ "ingredient" => $request->ingredient, 'title' => $request->title, 'portion' => $request->portion, 'how_to_1' => $request->how_to_1, 'how_to_2' => $request->how_to_2, 'how_to_3' => $request->how_to_3, 'how_to_4' => $request->how_to_4, 'how_to_5' => $request->how_to_5, 'how_to_6' => $request->how_to_6, 'how_to_7' => $request->how_to_7, 'how_to_8' => $request->how_to_8, 'how_to_9' => $request->how_to_9, 'how_to_10' => $request->how_to_10, 'how_to_11' => $request->how_to_11, 'how_to_12' => $request->how_to_12, 'one_point' => $request->one_point, ]); $order=Order::find($id); $order->recipes()->create([ "ingredient" => $request->ingredient, 'title' => $request->title, 'portion' => $request->portion, 'how_to_1' => $request->how_to_1, 'how_to_2' => $request->how_to_2, 'how_to_3' => $request->how_to_3, 'how_to_4' => $request->how_to_4, 'how_to_5' => $request->how_to_5, 'how_to_6' => $request->how_to_6, 'how_to_7' => $request->how_to_7, 'how_to_8' => $request->how_to_8, 'how_to_9' => $request->how_to_9, 'how_to_10' => $request->how_to_10, 'how_to_11' => $request->how_to_11, 'how_to_12' => $request->how_to_12, 'one_point' => $request->one_point, ]); return view("welcome"); }
試したこと
user(1)に対して order(=質問)(多)の関係
user(1)に対して recipe(=回答)(多)の関係
order(=質問)(1)に対して recipe(=回答)(多)の関係にて構築しようと思っております。
よって、orderとrecipeにはuserを識別するuser_id
を設けており、
recipeにはorderを識別するorder_id
を設けております。
ただ、order_id
が NOT NULL
が付与されているため、デフォルト値がないというエラーが起こります。
便宜的に
$request->user()->recipes()->create([
"ingredient" => $request->ingredient,
//途中省略//,
'order_id' => 1,
]);
と入力してみてもエラーは解消されず、
また次はMigration(Recipe)を$table->integer('order_id')->unsigned()->index()->nullable;
と変更すると
user_id
にデフォルト値がないというエラーが出ます。
補足情報(ツールのバージョンなど)
Laravel5.5
最後に
わかりづらい質問大変申し訳ございません。
お手数おかけしますがご教示いただければと存じます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/15 05:06
2019/08/15 09:42