Support the ongoing development of Laravel.io →
Database Eloquent

Dear All, I have a problem like that.

// First, I created one to many relation for Post and PostCategory

// app/model/Post

public function post_category() 
{
    return $this->belongsTo('PostCategory');
}


// app/model/PostCategory

public function posts()
{
    return $this->hasMany(Post);
}


// From my PostsController I can access properly for all Posts with category title
$data = Post::with('post_catgory')->get();    // this is fine


// But how can I grab data for single view? I tested like that.

$data = Post::find($id)->with('post_category')->get();    // doesn't work
$data = Post::find($id)->post_category;   // doesn't work too.

Actually I don't know how to retrieve. Please help.

Last updated 3 years ago.
0

Post::with('post_category')->find($id);

Last updated 3 years ago.
0

Thank you so much. It solved.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

webcrazy webcrazy Joined 26 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.