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

sarmadindhar's avatar

Access Data from 3 tables based on condition using eloquent orm

I have 3 tables in my Database

Category (id,name,status)

Subcategory(id,parent_category_id,name,status) (parent_category_id is passed as foreign key)

Post (id,title,subcategory_id,status) (sub_category_id passed as foreign key)

Know I want to make a relationship in as per following conditions The conditions are

  1. whenever the status of category is in_active the child subcategories and posts should not display
  2. Whenever the status of subcategory is in_active the all the posts having that subcategory_id should not display
  3. Whenever the post status in in_active that post will not display

I have used hasManythrough relation ship on my Category Model like: hasManyThrough("App\Product","App\Subcategory","parent_category_id","subcategory_id"); But it doesnt gives me the results that I want Is there a better solution ??????

0 likes
0 replies

Please or to participate in this conversation.