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

sarmadindhar's avatar

Data from 3 tables

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 b/w these tables as per following conditions

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

please suggest a good approach along with way to access the data

0 likes
1 reply
naeem_akhtar's avatar

$posts = Post::whereHas('sub_category', function($query){ $query->where('sub_category.status', '!==', 'in_active'); })->get();

maybe something like this to fetch post if there belonging sub_category has status not equal 'in_active'

here sub_category is relation function in post model defining hasMany relashion with posts.

Please or to participate in this conversation.