A
A
Alexander2016-09-20 12:13:23
Laravel
Alexander, 2016-09-20 12:13:23

Why is Laravel not displaying a post via BelongsTo()?

Good afternoon. I can't figure out relationships in Laravel.
Unable to select a record from the posts.slug column from the posts table

public function showSinglePost(Category $category, $slug)
    {

        $this->data['post'] = $category->belongsTo('App\Post', $slug, 'slug')->get();

        dd($this->data['post']);

    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Plisko, 2016-09-20
@AmdY

Write belongsTo in the model itself, and load via $category->load('post') if your category is already selected. Or $category->with('posts')->get() if the category is not loaded, because the relay needs to get data from the category itself in order to pull related posts.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question