Laravel Eloquent use the orWhere query in my search system request

  eloquent, laravel, laravel-query-builder, php, search

I tried to code a request with search system. Here the code:

$search = request()->get('search');
if(Auth::user()->hasRole('admin') || true) 
  list($orderBy, $orderDirection) = explode('.', request()->get('sort_by'));
  $prestations = Prestation::with(
  ->orWhere('service:name', 'regexp', "/$search/i")
  ->orderBy($orderBy, $orderDirection)

  $res = [
    'results' => $prestations,
    'total' => Prestation::all()->count(),

  return $res;

The problem is that I don’t know how to do something like I tried with the orWhere -> get all service name (from the relationship “with”) which are equal to my $search.

Thank you.

Source: Laravel StackOverflow

Leave a Reply