Add a relationship count / exists condition to the query.
use Illuminate\Database\Eloquent\Builder; $posts = Builder::table('posts') ->has('comments') ->get();
use Illuminate\Database\Eloquent\Builder; $users = Builder::table('users') ->has('articles', '>', 0) ->whereHas('articles', function($query){ $query->where('created_at', '>', now()->subMonth()); }) ->get();In example 1, the Builder queries the posts table and returns all records that have at least one comment in the comments table. In example 2, the Builder queries the users table and returns all records that have at least one article in the articles table where its created_at date is within the last month. The package library is "Illuminate\Database\Eloquent".