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();
2
  • don't you use ->limit(5) ? like bfore ->limit(5)->get() ? Commented Jan 21, 2020 at 15:05
  • Use ->take(10) -> get... See other question: stackoverflow.com/questions/35643192/… Commented Jan 21, 2020 at 15:06

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

thanks will the url look like this then /films?limit=5
This has nothing to do with urls. You need to update your url or get the value from the url manually
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();

4 Comments

thanks will the url look like this then /films?limit=10
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
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
btw there are two options on how you can get the value of limit you want to pass, just visit this article 5balloons.info/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.