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

MartinW12's avatar

How to get POST only data in Laravel 6

Hello,

I am using Laravel 6.

If I do a $request->post('foo') it returns the value of "foo" in the query string.

Similarly, if I do a $request->has('foo') it returns TRUE if "foo" is set in the query string.

However I want it to ONLY look at POST data, and to ignore the query string in both cases.

How can I make it do this?

Thanks,

Martin

0 likes
6 replies
Sergiu17's avatar

Hello, $request->post(), by calling post() it returns only data from body, ignoring those from query string

public function store(Request $request)
{
    dd($request->post());
}
MartinW12's avatar

Ok, just to clarify, I am talking about a GET route.

It does indeed seem as if in a POST route the post() method works as expected, but in a GET route the post() method returns the query string whereas it should in fact return an empty array/string or NULL.

I guess what I could do is call the query() method to see if post() is in fact returning the query string (for various reasons I can't use $request->method() to see if actual form data has been posted), but surely there is a better way?

Sergiu17's avatar

However I want it to ONLY look at POST data, and to ignore the query string in both cases.

I am talking about a GET route.

I can't understand, you can't look at the POST data in GET request, all I can say is that if you treat a GET and a POST request with the same method, then you need that if statement to check if it's a post request.

MartinW12's avatar
MartinW12
OP
Best Answer
Level 1

As you say, a GET request has no POST data so surely the "post()" method should return nothing.

Anyway, as you guessed, I have a trait function that has to work with both GET and POST requests, but cannot rely on $request->method() to determine which it is.

Either way, it appears as if that if statement is needed, so that is what I will do ...

Thanks :-)

1 like
sauravs012's avatar

Your code should be

$data = $request->request->get('form');
var_dump($data['name']);

Please or to participate in this conversation.