以下のように
Recipes.controllerとrecipes.showのレシピ詳細ページを書いているのですが
// getでrecipes/idにアクセスされた場合の「取得表示処理」 public function show($id) { $recipe = Recipe::find($id); $ingredients = Recipe::find($id)->ingredients; $how_to = Recipe::find($id)->how_to; return view('recipes.show',[ 'recipe' => $recipe, 'ingredients' => $ingredients, 'how_to' => $how_to, ]); }
//recipes.showのレシピ詳細ページ <div class="container"> <div class="row"> <div class="col-4"> <h2>{{ $recipe->name }}</h2> </div> <div class="col-7 offset-1"> <h3>{{ $recipe->content }}</h3> </div> </div> </div> <div class="container"> <div class="row"> <div class="col-4"> <img src="https://placehold.jp/200x200.png"></img> </div> <div class="col-8"> <h4>材料(2人分)</h4> <div class="row"> <div class="col-md-8"> <h4>材料・調味料名</h4> @foreach ($ingredients as $ingredient) <div> {{ $ingredient->ingredient }} </div> @endforeach </div> <div class="col-md-4"> <h4>分量</h4> @foreach ($ingredients as $ingredient) <div> {{ $ingredient->quantity }} </div> @endforeach </div> </div> </div> </div> </div> <div class="container"> <h4>作り方</h4> @foreach ($how_tos as $how_to) <div> {{ $how_to->how_to_make }} </div> @endforeach </div>
以下のようなエラーが起きて材料、分量、作り方が取得できません。
どのように変更すればいいのでしょうか?
1対多のリレーションは済んでいます。
namespace App; use Illuminate\Database\Eloquent\Model; class Recipe extends Model { protected $fillable = ['user_id', 'name', 'content', 'photo_url']; public function user() { return $this->belongsTo(User::class); } public function ingredient() { return $this->hasMany(Ingredient::class); } public function how_to() { return $this->hasMany(HowTo::class); } }
namespace App; use Illuminate\Database\Eloquent\Model; class Ingredient extends Model { protected $fillable = ['recipe_id', 'ingredient', 'quantity']; public function recipe() { return $this->belongsTo(Recipe::class); } }
namespace App; use Illuminate\Database\Eloquent\Model; class HowTo extends Model { protected $fillable = ['recipe_id', 'how_to_make']; public function recipe() { return $this->belongsTo(Recipe::class); } }
回答よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー