One possible solution to avoid the unnecessary is not null condition in the query is to use the whereNotNull method provided by Laravel's query builder.
Instead of defining the hasMany relation in your model, you can manually construct the query using the where and whereNotNull methods. Here's an example:
$relationId = x; // Replace x with the desired relation ID
$results = DB::table('table')
->where('relation_id', $relationId)
->whereNotNull('relation_id')
->get();
This will generate a query that only includes the where condition for the relation_id column, without the unnecessary is not null condition.
Remember to replace 'table' with the actual table name in your database.
By using the query builder directly, you have more control over the generated SQL queries and can avoid unnecessary conditions.