0

Is it necessary to write return keyword while using whereHas in laravel.

For eg. return $query->where('status', 1);

$posts = App\Post::whereHas('comments', function ($query) {
    $query->whereHas('content', function ($query){
       $query->where('status', 1);
    });
})->get();

Do we need to write return in every query callback?

sushant
  • 87
  • 7

3 Answers3

2

No, you're modifying the query builder instance passed to your closure by calling $query->where('status', 1);. Since objects are passed by reference and where() mutates this instance, there's no need to return anything.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
0

There is no need. Also you can use whereHas with nested relationship. Example:

$posts = App\Post::whereHas('comments.content', function ($query) {
    $query->where('status', 1);
})->get();
0

No. Because you have the same object (and modify it) inside every function. No need to return.

Petr
  • 420
  • 3
  • 13