dev.khosromanesh's avatar

whats diffrence between $request->name and $request->input(name)

hello every one, it was question for me whats the difference between the $request->name and $request->input('name') ? must we use the second for insert from forms ?

0 likes
15 replies
Sinnbeck's avatar

It is in theory the same. Personally I prefer using the methods but the request class calls the same method under the hood

1 like
Sinnbeck's avatar

@dev.khosromanesh yup.. You can in theory run into using attributes that already exists with the attribute version though

1 like
rodrigo.pedra's avatar
1 like
rodrigo.pedra's avatar

@Sinnbeck it is the same thing as using semicolons on JS. Although I will admit JS interpreters handle ASI rules much better these days, I prefer to be explicit than remember possible glitches and corner cases.

But as with my opinion on docker earlier, i make no judgment on those who prefer implicit things (a.k.a. magic).

And of course, when working as a contractor on an existing project I always stick to the current code-style.

1 like
Sinnbeck's avatar

@rodrigo.pedra true. And I like alot of the magic in eloquent models. But there I feel in control. With requests it feels more like a black box, so I prefer methods that tell me what they do. Like ->boolean() or ->file()

2 likes
rodrigo.pedra's avatar

@Sinnbeck you are right, I mostly forgot about Eloquent, as it is already so intrinsic in my muscle memory .

I learned about ->boolean() on requests relatively recent, and it is a nice addition, much simpler.

On Eloquent, aside from attributes and relation I also tend to avoid much magic, such as ->whereName(), ->whereEmail(), etc.

But of course, setting and getting attribute and relations manually would be much more work and pointless.

1 like
jlrdw's avatar

It all goes to symfony as a normal post or get array, and symfony breaks it down, i.e., handles if a put request behind the scenes.

1 like
rodrigo.pedra's avatar

@jlrdw

The underlying Symfony request does not know about Laravel resolved route parameters.

And the ->input() method reaches for the underlying $request bag, which separates regular input from file input.

But for 99% cases it can be considered equivalent, as for Laravel being able to resolve route parameters you must provide them on the action signature, so there is no point on accessing it from the Request object as a magic property in a controller.

And most tutorials use the ->file(...) method for uploaded files , so it is uncommon to see them accessed as a magic property in the wild.

I just wanted to point out that although looking equivalent they are not exactly the same.

1 like
Sinnbeck's avatar

@rodrigo.pedra as someone who has replaced native $_GET and $_POST throughout an old project (with symfony Http foundation), I know all too well how different it to work with. But at least it feels similar to $_GET and $_POST

1 like
jlrdw's avatar

Thanks @rodrigo.pedra for explaining, and yes I know file is handled different, in fact behind the scenes, symfony uses the regular php move_uploaded_file.

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

$request->name is a convenient shortcut, but the input method has advantages in some cases.

  • when the form input includes a dash, eg full-name. It is not possible to access $request->full-name but $request->input('full-name') will not complain about the dash

  • $request->input() method allows a fallback to be specified if the field is missing from the request, eg $request->input('type','invoice')

  • when dealing with arrays, in the input, you can use dot to dig into the request, eg $request->input('products.0.name');

4 likes

Please or to participate in this conversation.