0

i have a query which filters films based upon the input by the user, i am needing to add a limit as a query parameter so it limits the amount of films that are returned, how do i add this in, i am new to writing eloquent queries.

query

$films = Film
::when($request->input('first'), function ($query, $first) {
    $query->whereHas('options', function ($query) use ($first) {
        $query->where('first', $main);
    });
})
->when($request->input('second'), function ($query, $second) {
    $query->whereHas('options', function ($query) use ($second) {
        $query->where('second', $second );
    });
})
->when($request->input('age'), function ($query, $age) {
    $query->whereHas('ageRatings', function ($query) use ($age) {
        $query->where('age', $age);
    });
})
->when($request->input('country'), function ($query, $country) {
    $query->whereHas('locations', function ($query) use ($country) {
        $query->where('country', $country);
    });
})
->when($request->input('city'), function ($query, $city) {
    $query->whereHas('locations', function ($query) use ($city) {
        $query->where('city', $city);
    });
})
->get();
lilnoobcoder
  • 85
  • 1
  • 11

2 Answers2

1

You can use $query->limit(5) or $query->take(5).

If you want to skip the first 5 results, use $query->skip(5)

If you want to add pagination, you can use $query->paginate(5)

See More: Query Builder

Patrick Schocke
  • 1,493
  • 2
  • 13
  • 25
0

You can use both of these method

the Limit method

$limit = 10;
$films = Film
::when($request->input('first'), function ($query, $first) {
    $query->whereHas('options', function ($query) use ($first) {
        $query->where('first', $first);
    });
})
->when($request->input('second'), function ($query, $second) {
    $query->whereHas(‘options', function ($query) use ($second) {
        $query->where('second', $second);
    });
})
->when($request->input('age'), function ($query, $age) {
    $query->whereHas('ageRatings', function ($query) use ($age) {
        $query->where('age', $age);
    });
})
->when($request->input('country'), function ($query, $country) {
    $query->whereHas('locations', function ($query) use ($country) {
        $query->where('country', $country);
    });
})
->when($request->input('city'), function ($query, $city) {
    $query->whereHas('locations', function ($query) use ($city) {
        $query->where('city', $city);
    });
})
->limit($limit)
->get();

the Take method

$limit = 10;
    $films = Film
    ::when($request->input('first'), function ($query, $first) {
        $query->whereHas('options', function ($query) use ($first) {
            $query->where('first', $first);
        });
    })
    ->when($request->input('second'), function ($query, $second) {
        $query->whereHas('options', function ($query) use ($second) {
            $query->where('second', $second);
        });
    })
    ->when($request->input('age'), function ($query, $age) {
        $query->whereHas('ageRatings', function ($query) use ($age) {
            $query->where('age', $age);
        });
    })
    ->when($request->input('country'), function ($query, $country) {
        $query->whereHas('locations', function ($query) use ($country) {
            $query->where('country', $country);
        });
    })
    ->when($request->input('city'), function ($query, $city) {
        $query->whereHas('locations', function ($query) use ($city) {
            $query->where('city', $city);
        });
    })
    ->take($limit)
    ->get();
lilnoobcoder
  • 85
  • 1
  • 11
Qonvex620
  • 3,819
  • 1
  • 8
  • 15
  • thanks will the url look like this then `/films?limit=10` – lilnoobcoder Jan 21 '20 at 15:17
  • `get the value of limit in url` do you mean this solution you provided will return the above url or am i needing to get something else – lilnoobcoder Jan 21 '20 at 15:29
  • no it does not, you have to get first the value of limit in your url and initialize it to a variable before you execute the query – Qonvex620 Jan 21 '20 at 15:33
  • btw there are two options on how you can get the value of limit you want to pass, just visit this article https://www.5balloons.info/how-to-get-url-parameters-into-controller-laravel/ – Qonvex620 Jan 21 '20 at 15:38