1

I am using laravel 5.3 and need a bit of help with Eloquent model queries. I have three models (UserDetails, Categories, Articles). I have a relationship between UserDetails->Categories (belongstoMany), and a relationship between Categories->Articles (belongstoMany) which work well. However how would I go about getting the relationship data between Userdetails->Categories->Articles.

Each individual relationship is working fine i.e. Userdetails::find(1)->categories and Categories::find(1)->Articles.

I have a feeling that scopes may be the answer but they don't seem to work when I've attempted it.

Relationships in models

UserDetails.php

 public function Categories(){
    return $this->belongstoMany('App\Categories', 'users_cats',  'user_id','cat_id');
}

Categories.php

  public function articles(){
    return $this->belongsToMany('App\Article', 'article_categories', 'categoryID', 'articleID');
}

Ive looked into HasManyThrough function but again, I'm having issues implementing it, as far as I can see it should be

  return $this->hasManyThrough('App\Article', 'App\Categories', TertiaryForeignKey, FinalForeignKey, LocalForeignKey);

My tables are set up as

articles_categories pivot table

articleID – primary key of the article

categoryID – primary key of the category

users_cats pivot table

user_id – primary key of the userdetails

cat_id – primary key of the categories

Based on this it the hasManyThrough should look like this?

 public function articles(){
    return $this->hasManyThrough('App\Article', 'App\Categories', 'user_id', 'articleID', 'id');
}

however this returns the error Column not found: 1054 Unknown column 'categories.user_id' in 'field list'

2 Answers 2

0

update

So if you want to have this kind of relationship

userdetails->categories->articles

then you need to make this:

Userdetail model:

public function categories()
{
    return $this->hasMany(Category::class);
}

public function articles()
{
    return $this->hasManyThrough(Article::class, Categories::class);
}

Category model:

public function userdetails()
{
    return $this->belongsTo(Userdetails::class);
}

public function categories()
{
    return $this->hasMany(Category::class);
}

Article model:

public function categories()
{
    return $this->belongsToMany(Category::class);
}

Then you can call UserDetails::find(1)->articles->get(); directly

Sign up to request clarification or add additional context in comments.

6 Comments

I am using a custom able structure for my relationship data, and that returns the error SQLSTATE[42S22]: Column not found: 1054 Unknown column 'categories.user_details_id' in 'field list' (SQL: select journalarticle.*, categories.user_details_id from journalarticle inner join categories on categories.id = journalarticle.categories_id where categories.user_details_id = 1) Do I need to pass my keys to the HasManyThrough function? Ive added my table structure to the original post
you should always name your columns by following the convention, that is categorie_id, article_id, user_id when creating foreign keys....and then you don't need to specify those keys when you are creating relationships, just name the class and that's it. It is much simpler and it should be done that way!
I fixed the tables but still it's returning that error. Is the issue of running a hasThrough many with two many to many relationships?
Oh yes...you need to make the other relationships as well...i will update my answer
i would recommend to change the model name from Category to Tag because the laravel works automatically with naming the table by adding "s" to the end of the Model name....
|
0

You just need to declare the relationship like this in the UserDetails.php model:

public function categories()
{
    return $this->hasMany(Categories::class);
}

in Categories.php model:

 public function articles()
{
    return $this->hasMany(Articles::class);
}

Then you can retrieve the collection of categories in your controller:

$userdetails = UserDetails::get();

pass that $categories variable into your View and display each record with an foreach loop (where the articles is the function in your model)

   @foreach($userdetails->categories as $usercategories )
        <div> {{$usercategories->name}} </div>

        @foreach($usercategories->articles as $categoryarticles )
          <div> {{$categoryarticles->name}} </div>
        @endforeach
   @endforeach

with the second foreach you will access the articles of the categories that belongs to the user.

2 Comments

Yes I can get the Articles associated with a category fine, as per the original question I want to get all the articles associated with a category which the user has so UserDetails->Categories->Articles, not Categories->Articles.
I have edited my answer i hope it helps you @user5067291

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.