Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

makapaka's avatar

Get request->all without query params

When I do this

$req = $request->all();

I just want to get the request body, but this is also adding any inline params I might add, for eg this ?q=1 is also in the request array that comes back.

Whats the proper way to do it, so it ignores query params ?

0 likes
8 replies
Tray2's avatar

You use validation,

$valid = $request->validate([
	'name' => 'required',
	'age' => 'required'
	'gender' => 'required'
 ]);

Then you will only get the inputs you want.

jlrdw's avatar

You normally have query parameters when paginating, so they are carried forward. And I advise not using $request->all() rather request each one at a time, but just my opinion.

Usually $request->all() is used in a post request.

makapaka's avatar

i think more info necessary as to show why I do request->all()

My request body is something like this

[ 
    {
        id: 1,
        order: 1
     },
     {
         id: 2,
         order: 2
      }
      ...
]

So based on some rules, I am validating the exact number of elements coming in the request, so that I don't do anything if its incorrect.

if (count($request->all()) !== $countRequired)
    throw new Exception..

So, as you can see, if I add a query param, and that is added to the request, this validation will fail. I was hoping it would not be necessary to strip the param programmatically before doing the validation.

Sinnbeck's avatar

@makapaka Sounds like it could be easily solved by just putting the data in a key?

if (count($request->get('data')) !== $countRequired)
    throw new Exception..
1 like
makapaka's avatar

@Sinnbeck yes fair point - probably why I havent seen this issue before. Bummer because I didnt want to change the structure of the api at this point ...

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@makapaka Could you perhaps show how the data is sent in?

Another idea. Add the keys you dont want in here

if (count($request->except(['_token'])) !== $countRequired)
1 like
Sinnbeck's avatar

@makapaka if you end up using either solution, please mark it as best answer to set the thread as solved

Please or to participate in this conversation.