SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'union (select `accepter` as `name` from `eggfriends` where `requester` = ? and `' at line 1 (SQL: select count(*) as aggregate from `users` inner join `trainers` on `users`.`id` = `trainers`.`user_id` inner join `eggfriends_count` on `trainers`.`name` = `eggfriends_count`.`name` where `trainers`.`name` not in ((select `eggfriends_count`.`name` from `eggfriends_count` inner join `trainers` on `trainers`.`name` = `eggfriends_count`.`name` where `eggfriends_count` >= eggfriend_cap) union (select `accepter` as `name` from `eggfriends` where `requester` = JBomb2411 and `accepted` != 0) union (select `trainers`.`name` from `trainers` where `user_id` = 1)))

commenting out the paginate and uncommenting the toSql / dd I get the following query (formatted for readability/better error line reporting):

After replacing the questionmark after requester = ? with a proper value phpmyadmin spits out the following error when running this query:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION
(SELECT accepter AS name
' at line 19.
Removing all the parenthesis around the selects inside the NOT IN resolves the problem and the query completes. So my question is how do I keep laravel from inserting parenthesis around union queries?