pc579's avatar
Level 1

$request question

Edit : see later, my projects are now with the same Laravel version 9.26.1.

I have two laravel/vue.js projects. One with Laravel 9.2 et the second with Laravel 9.11. NO SEE edit above.

I got a difference using $request→mykey and $request→get(‘mykey’).

The data send by vues.js (axios) have the same structure.

I dumped $request at the beginning of my controller and found some differences (see below) that explain the problem but I don’t undertand why these differences. For now I cannot make the two projects with the same version, so I would like to understand how $request is build inside Laravel.

2 main differences

  • mykey is not present in the « InputBag »
  • json return ParamaterBag (Projet 1) et InputBag (Projet2)

Projet 1 (Laravel 9.2)

Illuminate\Http\Request^ {#46
  +attributes: Symfony\Component\HttpFoundation\ParameterBag^ {#48
    #parameters: []
  }
  +request: Symfony\Component\HttpFoundation\InputBag^ {#38
    #parameters: array:7 [
      …					// mykey not present
    ]
  }
…

  -isForwardedValid: true
  #json: Symfony\Component\HttpFoundation\ParameterBag^ {#45
    #parameters: array:10 [
       ...
      "mykey" => "1"				// my key present here
    ]
  }
  #convertedFiles: []
...

Projet 2 (Laravel 9.11)

Illuminate\Http\Request^ {#44
  +attributes: Symfony\Component\HttpFoundation\ParameterBag^ {#46
    #parameters: []
  }
  +request: Symfony\Component\HttpFoundation\InputBag^ {#36
    #parameters: array:10 [
      ….
      "mykey" => 1
    ]
  }
…

  -isForwardedValid: true
  #json: Symfony\Component\HttpFoundation\InputBag^ {#36}
  #convertedFiles: []
...

How can we explain these differences ?

How $request is build inside Laravel ?

Thanks in advance for any help.

0 likes
4 replies
martinbean's avatar

@pc579 You‘re using two different versions. Things are going to be different between those two versions. You may have found one of those things that is different.

You should be explicit about where you’re retrieving data from. If you want request (POST) data, use $request->input('foo'). If you’re expecting a certain data type (i.e. a string or an integer), then cast the value to that data type: (int) $request->input('foo')

Although you should be validating data before this to ensure you’re getting data of the type you expect, i.e. a string or an integer.

1 like
pc579's avatar
Level 1

Thanks @martinbean for your reply.

I totally agree with you but, I did not see any difference in my code and data that explains why $request->get('mykey') retuns null in my project 1. I continue by analyzing deeper in the request classes ...

pc579's avatar
Level 1

@martinbean

I come back after deeper analysis. First my projects are now in the same version, Laravel 9.26.1. I did not find the root cause of the problem but found that in one case the magic fonction __toString of « vendor/symfony/http-foundation/Request.php » is called and not in the other case. I did not understand why this function is called since I don’t cast $request to string. (same server, same controller).

I open a second post to split the question https://laracasts.com/discuss/channels/laravel/why-tostring-is-called

pc579's avatar
pc579
OP
Best Answer
Level 1

Solved the problem with "composer update" , but the question with __toString is still opened.

Please or to participate in this conversation.