'Join one to many relationship tables and return all record using laravel eloquent

I have two tables users and posts. users has one to many relation with posts table. Post has 'type' column. This type may be sports,art, politics... etc.

users table - id, name, account_type, email

account_type may be active, test, blacklisted ... etc

posts table - id, user_id, name, type, date

Here I need to get all 'politics' posts of all 'active' players posted after a particular 'date'

How to get this using laravel eloquent way?

I have below function in User model class

public function post()
{
    return $this->hasMany( Post::class );
}


Solution 1:[1]

You may try this

Post::join('users','posts.user_id','=','users.id')
    ->where('posts.type','=','politics')
    ->where('users.account_type','=','active')
    ->where('posts.date','=','Your selected date')
    ->select('posts.*')
    ->get();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 beibei