skip to Main Content

I am new in Laravel, so maybe this is a dumb question.

    $param = $req->input('param');
    $param = $req->param;

Both of them show the same result. The first one is what i learn from most site in internet. Is there any difference between them? Which one is better?

2

Answers


  1. Both $req->input(‘param’) and $req->param are used to retrieve input data from the request.

    $req->input(‘param’) is more explicit and indicates that you are specifically retrieving a value from the input. It also allows you to specify a default value to be returned if the parameter is not found like this

    $param = $req->input('param', 'default');
    

    This second argument will be returned if the parameter is not found in the request.

    $req->param
    is more concise and can be used as a shortcut for retrieving a parameter from the request and does not allow you to specify a default value.

    There is no functional difference between the two methods. You can use either $req->input(‘param’) or $req->param based on your preference. The $req->param is just a shorthand notation for the $req->input(‘param’). Choose the one that you find more readable and consistent with your coding.

    Login or Signup to reply.
  2. Both are used to retrieve input data from the request.

    The only difference is that you can define the default value using $req->input().

    example :

    $param = $req->input('param','default');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search