How to do a inner query in laravel. Please consider that my issue is other than this question.
I have a common method that returns both common and complex inner query. With it, I've found a dirty fix reading laravel's source code
$query->whereIn('column', function ($query) { /*noop*/ });
$innerQuery = Model::myComplexInnerQueryMethod()->getQuery();
$query->getQuery()->wheres[0]['query'] = $innerQuery;
$query->mergeBindings($innerQuery);
But this is ugly (to say the least) Any thoughts?
Update for martin's answerclass MyModel extends \Eloquent {
// @return \Illuminate\Database\Query\Builder
static function myComplexInnerQueryMethod($index) {
return static::query()->join(…)->join(…)->where(…)->where(…)->etc();
}
}
class Analytics extends MyModel {
function scopeIndex($query, $index) {
$query->whereIn('column', function ($query) { /*noop*/ });
$innerQuery = self::myComplexInnerQueryMethod($index)->getQuery();
$query->getQuery()->wheres[0]['query'] = $innerQuery;
$query->mergeBindings($innerQuery);
return $query;
}
}