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

Geoffrey06's avatar

Relation between tree table

Hi guys !

I need some advice , i have three table

Table

users

  • id

in_stocks

  • id
  • user_id
  • article_id

articles

  • id

relation between tables

  • user hasMany in_stocks
  • in_stock belongsTo one articles
  • article hasMany in_stocks

But if i need to extract the list of article add in stock by a specific user like :

User::find(1)->inStock->articles Wich relation can i use ????

0 likes
1 reply
usama.ashraf's avatar

I've never used hasManyThrough for this kind of a structure, but try this in the User model:

public function articles()
{
    return $this->hasManyThrough(Article::class, InStock::class, 'article_id', 'id', 'user_id');
    // or: $this->hasManyThrough(Article::class, InStock::class, 'user_id', 'id', 'article_id');
}

Please or to participate in this conversation.