Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sajib007's avatar

problems to retreive data from hasManyThrough relationship in laravel 4.2

I have 3 tables in my Database:

  1. posts contains(id, title)
  2. categories (id, name),
  3. categories_posts (post_id, categories_id).

I am using laravel 4.2. That are my Models:

Post.php

protected $table = 'posts';
public function categories_posts()
{
    return $this->hasMany('Categories_post', 'post_id');
}

public function categories()
{
    return $this->hasManyThrough('Categories', 'Categories_post');
}

Categories.php

protected $table = 'categories';

protected $fillable = ['name', 'description'];

public function categories_posts()
{
    return $this->hasMany('Categories_post', 'categories_id');
}

public function posts()
{
    return $this->hasManyThrough('Post', 'Categories_post');
}

Categories.php

protected $table = 'categories_posts';

protected $fillable = ['categories_id', 'post_id'];

public function categories()
{

    return $this->belongsTo('Categories');
}

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

PostsController.php

public function index()
{
    $posts = Post::with('author')->get();
    return View::make('posts.index', (
        'posts' => $posts);
}

shortly presents main code index.blade.php

@foreach ($posts as $post)
  <tr>
   <td>{{ $post->created_at }}</td>
   <td>{{ $post->title }}</td>
   <td>{{ $post->author->display_name }}</td>
   @foreach ($post->categories->categories_post as $cat)
     <td>{{ $cat->name }}</td><!-- how to get category name associated with posts !>
   @endforeach
   <td>{{ $post->status }}</td>
   <td>{{ $post->comments_count }}</td>
  @endforeach

That code returns errors, I don't understand.

So how to get a category name, which is related to posts by hasManyThrough relation.

Suggest me if there is a better way, than make use of the hasManyThrough relation.

0 likes
8 replies
JarekTkaczyk's avatar
Level 53

@sajib007 It's belongsToMany (m-m relation) not hasManyThrough.

And you don't need that Categories_Post model, just:

// Post model
public function categories()
{
    return $this->belongsToMany('Category', 'categories_posts', 'post_id', 'categories_id');
}

// Category model
public function posts()
{
    return $this->belongsToMany('Post', 'categories_posts', 'categories_id', 'post_id');
}

// then just
$category->posts; // collection of Post models
$post->categories; // collection of Category models

And I suggest sticking to one convention, ie. post_id and category_id, not categories_id.

1 like
sajib007's avatar

but belongsToMany also does not work for me....why? i dont understand why? in laravel.com there is hasManyThrough relationship...@JarekTkaczyk

JarekTkaczyk's avatar

@sajib007 Be more specific - I showed you exactly what you need. If anything doesn't work, then show us the code and say what error you are getting. Otherwise you're making it hard to help you out..

1 like
sajib007's avatar

@JarekTkaczyk...thanks a lot.it works like a charm...there was my little silly mistake that's why it does not work....but you are superb....many many thanks to u, i passed whole day long to find it out but i don't.....thanks again...

JarekTkaczyk's avatar

@sajib007 I'm glad. Just remember - it's really easy to help you when you say exactly what you did, what you expected and what you got instead.

sajib007's avatar

as i lost whole day long for the answer through google but did not get the answer, i was really upset....as i am trying my best to learn laravel and as a new learner i face so many problems regarding this....can u suggest me for the laravel query for the best resource with details its meaning like how it works...thanks..@JarekTkaczyk

Please or to participate in this conversation.