N
N
Nikolay2018-08-31 17:22:29
Laravel
Nikolay, 2018-08-31 17:22:29

Why is an exception thrown when using @foreach?

Good day)
Essence: We have several models:

# Recipe
class Recipe extends Model
{
    public function tags()
    {
        return $this->belongsToMany('App\Models\Tag');
    }

    public function ingredients()
    {
        return $this->belongsToMany('App\Models\Ingredient')->withPivot('amount');
    }

    public function getRouteKeyName()
    {
        return 'slug';
    }
}

# Tags
class Tag extends Model
{
    protected $fillable = [
        'name', 'slug', 'subsection_id', 'tag_type_id',
    ];

    public function recipes()
    {
        return $this->belongsToMany('App\Models\Recipe');
    }
}
...

Super simple controller:
public function show(Recipe $recipe)
    {
        return view('pages/recipes/show', compact('recipe'));
    }

In the blade template, when iterating over , everything works fine. But as soon as I add it , an error occurs: If you call it in the controller , everything is fine. If you write in the template - everything shows / works. What could be the problem? @foreach($recipe->ingredients as $ingredients)
@foreach($recipe->tags as $item)
5b894cf62ea5f901003234.png
dd($recipe->tags)
{{ var_dump($recipe->tags) }}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Hog, 2018-08-31
@iNickolay

Typo or missing design@endforeach

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question