So I have the following code:
public function scopePopular($query, $limit = 10)
{
$query->leftJoin(DB::raw('(SELECT COUNT(id) visits, UNIQUE(ip_address) FROM `visitors`) visitors'), function($join)
{
$join->on('visitors.visitorable_id', '=', 'db885a18-f0b7-4f55-9d93-743fbb5d9c94');
$join->where('visitors.visitorable_type', '=', 'App\Profile');
});
return $query;
}
which produces the following query:
select * from `profiles` inner join `profile_genres` on `profiles`.`id` = `profile_genres`.`profile_id` left join (SELECT COUNT(id) visits, UNIQUE(ip_address) FROM `visitors`) visitors on `visitors`.`visitorable_id` = `db885a18-f0b7-4f55-9d93-743fbb5d9c94` and `visitors`.`visitorable_type` = ? where `profile_genres`.`genre_id` = ? and `profiles`.`deleted_at` is null
This query doesn't appear to work, I'm guessing due to a syntax error and MYSQL isn't exactly specific in its error messages.
SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNIQUE(ip_address) FROM
visitors
)onvisitors
.visitorable_id
= 'db885a18-f0' at line 1