I have manually built a large query using the various eloquent functions (i.e. $this->newQuery()->join....), but I can't get it to run.
When I call
echo $query->toSql();
It shows me the query. I can copy and paste it into my command line mysql client, and it runs fine, and returns several rows. But when I call
echo $query->count();
or
echo $query->get()->count();
It's showing 0.
I eventually enabled my mysql general log to see what was happening. I see that as laravel runs, it executes several queries - each has a prepare
line in the log, followed by an execute
line. But this one doesn't.
It appears that laravel is preparing the statement, but never executing it. Why not?
After much testing, I have identified the line that causes the problem:
$query->having('book_author_author_id', 'NOT IN', DB::raw('('.implode(',',$author_ids).')'));
It appears that queries which contain a 'having' clause are not executed by laravel, instead it pretends to execute them and returns an empty collection. What is going on?