I would know how to perform an 'Advanced Where'. I didn't find anything in the documentation that explain what I want.. even there. (cf: http://laravel.com/docs/4.2/queries#advanced-wheres).
Post::whereHas('international_post_en', function($q) {
$q->where('is_published', 1);
})->whereHas('categories', function($q) {
$q->where('name', 'test-one');
})->orWhereHas('subcategories', function($q) {
$q->where('name', 'test-two');
})->with('categories', 'subtags')
->get();
My query look like this:
select * from `posts` where `posts`.`deleted_at` is null and (select count(*) from `international_posts_en` where `international_posts_en`.`posts_id` = `posts`.`id` and `is_published` = ?) >= 1 and (select count(*) from `categories` inner join `posts_has_categories` on `categories`.`id` = `posts_has_categories`.`categories_id` where `posts_has_categories`.`posts_id` = `posts`.`id` and `name` = ?) >= 1 or (select count(*) from `subcategories` inner join `posts_has_subcategories` on `subcategories`.`id` = `posts_has_subcategories`.`subcategories_id` where `posts_has_subcategories`.`posts_id` = `posts`.`id` and `name_en` = ?) >= 1
But I want my query to look like this:
select * from `posts` where `posts`.`deleted_at` is null and (select count(*) from `international_posts_en` where `international_posts_en`.`posts_id` = `posts`.`id` and `is_published` = ?) >= 1 and [(](select count(*) from `categories` inner join `posts_has_categories` on `categories`.`id` = `posts_has_categories`.`categories_id` where `posts_has_categories`.`posts_id` = `posts`.`id` and `name` = ?) >= 1 or (select count(*) from `subcategories` inner join `posts_has_subcategories` on `subcategories`.`id` = `posts_has_subcategories`.`subcategories_id` where `posts_has_subcategories`.`posts_id` = `posts`.`id` and `name_en` = ?) >= 1[)]
(Sorry It is not very readable)
You can see the changes within the brackets. So, I want the whereHas and the orWhereHas clause to be grouped within parenthesis.
Is it possible with the Laravel Query Builder or should I make a handmade query?
Thank you in advance.