Answer the question
In order to leave comments, you need to log in
How to make friends in Laravel 5 where()->orWhere and whereIn()?
I am fetching from a database. From the table I select posts of the user - public and private. Then I want to select posts from these posts only in certain categories.
$posts = Posts::where('status', 1)->orWhere(function($user_post){
$user_post->where('status', 3)
->where('public', 0);
});
//какой-то код где получаем id категорий
$cats_id = [3, 7, 12 ,15];
$posts = $posts->whereIn('cat_id', $cats_id)->get();
Answer the question
In order to leave comments, you need to log in
look, now your builder does the following `Condition_1 OR Condition_2 AND Condition 3`
You want it to be `(Condition_1 OR Condition_2) AND Condition 3`
"Parentheses" in the Laravel builder are added using an anonymous name.
$posts = Posts::where(function ($q) {
$q->where('status', 1)
->orWhere(function($user_post){
$user_post->where('status', 3)->where('public', 0);
});
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question