以下はレシピコントローラーのeditとupdateの部分
RecipesController
1 public function edit($id) 2 { 3 $recipe = Recipe::find($id); 4 $ingredient = Recipe::find($id)->ingredient; 5 $how_to = Recipe::find($id)->how_to; 6 7 return view('recipes.edit',[ 8 'recipe' => $recipe, 9 'ingredients' => $ingredient, 10 'how_tos' => $how_to, 11 ]); 12 }
RecipesController
1// putまたはpatchでrecipes/idにアクセスされた場合の「更新処理」 2 public function update(Request $request, $id) 3 { 4 $recipe = Recipe::find($id); 5 $ingredient = Recipe::find($id)->ingredient; 6 $how_to = Recipe::find($id)->how_to; 7 $recipe->name = $recipe->name; 8 $recipe->content = $recipe->content; 9 $ingredient->ingredient = $ingredient->ingredient; 10 $ingredient->quantity = $ingredient->quantity; 11 $how_to->how_to_make = $how_to->how_to_make; 12 $recipe->save(); 13 14 return back(); 15 }
以下は編集ページ
edit
1<div class="container"> 2 <div class="row"> 3 <div class="col-3"> 4 {!! Form::model($recipe, ['route' => ['recipes.update', $recipe->id], 'method' => 'put']) !!} 5 <div class="form-group"> 6 {!! Form::label('name', 'レシピ名') !!} 7 {!! Form::text('name', null, ['class'=> 'form-control']) !!} 8 </div> 9 </div> 10 <div class="col-6 offset-3"> 11 <div class="form-group"> 12 {!! Form::label('content', 'ひとこと') !!} 13 {!! Form::text('content', null, ['class'=> 'form-control']) !!} 14 </div> 15 </div> 16 </div> 17 </div>
edit
1<div class="container"> 2 <div class="row"> 3 <div class="col-4"> 4 <img src="https://placehold.jp/200x200.png"></img> 5 </div> 6 <div class="col-8"> 7 <h3>材料(2人分)</h3> 8 <div class="row"> 9 <div class="col-md-8"> 10 <div class="form-group"> 11 {!! Form::label('ingredient', '材料・調味料名') !!} 12 <?php 13 for($i = 0; $i <=7; $i++){ 14 ?> 15 {!! Form::text("ingredients[$i][ingredient]", null, ['class'=> 'form-control']) !!} 16 <?php } ?> 17 </div> 18 </div> 19 <div class="col-md-4"> 20 <div class="form-group"> 21 {!! Form::label('quantity', '分量') !!} 22 <?php 23 for($i = 0; $i <=7; $i++){ 24 ?> 25 {!! Form::text("ingredients[$i][quantity]", null, ['class'=> 'form-control']) !!} 26 <?php } ?> 27 </div> 28 </div> 29 </div> 30 </div> 31 </div> 32 </div>
edit
1<div class="container"> 2 <h3>作り方</h3> 3 <div class="row"> 4 <?php 5 for($i = 0; $i <=7; $i++){ 6 ?> 7 <div class="col-md-3"> 8 {!! Form::label('how_to_make', $i+1) !!} 9 {!! Form::textarea("how_to[$i][how_to_make]", null, ['class'=> 'form-control']) !!} 10 </div> 11 <?php } ?> 12 </div> 13 </div> 14 <div class="container"> 15 <div class="row"> 16 <div class="col-6"> 17 {!! Form::submit('更新', ['class' => 'btn btn-dark']) !!} 18 {!! Form::close() !!} 19 </div> 20 </div> 21 </div> 22</div>
以下は3つのデーターベース
Recipe
1namespace App; 2 3use Illuminate\Database\Eloquent\Model; 4 5class Recipe extends Model 6{ 7 protected $fillable = ['user_id', 'name', 'content', 'photo_url']; 8 9 public function user() 10 { 11 return $this->belongsTo(User::class); 12 } 13 14 public function ingredient() 15 { 16 return $this->hasMany(Ingredient::class); 17 } 18 19 public function how_to() 20 { 21 return $this->hasMany(HowTo::class); 22 } 23}
Ingredient
1namespace App; 2 3use Illuminate\Database\Eloquent\Model; 4 5class Ingredient extends Model 6{ 7 protected $fillable = ['recipe_id', 'ingredient', 'quantity']; 8 9 public function recipe() 10 { 11 return $this->belongsTo(Recipe::class); 12 } 13}
HowTo
1namespace App; 2 3use Illuminate\Database\Eloquent\Model; 4 5class HowTo extends Model 6{ 7 protected $fillable = ['recipe_id', 'how_to_make']; 8 9 public function recipe() 10 { 11 return $this->belongsTo(Recipe::class); 12 } 13}
レシピ名・ひとこと・作り方は編集画面に反映されていますが、材料と分量の部分が反映されません。
1対多の関係で配列で格納したDBからうまく引っ張ってこれていないのでしょうか?
また、更新ボタンを押してもエラーが発生し、うまく更新されません。
更新の際のエラーはこちらです。
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ingredient' in 'field list' (SQL: update `recipes` set `updated_at` = 2018-12-29 13:42:36, `ingredient` = [], `quantity` = , `how_to_make` = where `id` = 27)
どこがおかしいのでしょうか?
あなたの回答
tips
プレビュー