Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Request default in Laravel 5.1 when none is given

I am using Laravel 5.1 and I need to make a default value when in field is none given.

So the view looks like:

<form name="search" method="POST" action="/s">
    Country<input name="country" type="text">
    Field<input name="field" type="text">
    <button type="submit" type="button">Search</button>
    <input name="_token" value="{{ csrf_token() }}" type="hidden">
</form>

The controller looks like:

public function extendedPost(Request $request){
        $data = $request->all();
        $request->input('field', 'Random');
        dd($data);
}

In Laravel 5.1 Documentation we have:

You may pass a default value as the second argument to the input method. This value will be returned if the requested input value is not present on the request:

$name = $request->input('name', 'Sally');

So when I have something in "Field" I have:

array:21 [▼
  "country" => ""
  "field" => "dddd"
  "_token" => "XeMi"
]

But when the "Field" is empty I have

 array:21 [▼
      "country" => ""
      "field" => ""
      "_token" => "XeMi"
    ]

Instead of

 array:21 [▼
      "country" => ""
      "field" => "Random"
      "_token" => "XeMi"

Thanks for helping me

like image 905
m7vm7v Avatar asked Oct 25 '25 23:10

m7vm7v


2 Answers

Very late answer here, but hopefully still useful clarification.

You may pass a default value as the second argument to the input method. This value will be returned if the requested input value is not present on the request:

This means the second argument will be used if the input isn't present. But if this comes from an optional field in a form, then the input IS present - with the value of an empty string.

That means that

$name = $request->input('name', 'Sally');

Will return "Sally" when there is no name field submitted by the form, but will return "" if the user leaves the name field blank.

If we want the value of 'Sally' when the field was blank, we can do as Zakaria suggests and use:

if( $request->has('field')) {
  $request->field = 'Random';
}

... but that will very quickly become messy if you have a lot of fields that all need to be checked.

Another option is to use the ternary operator. Something like:

$yourValue = $request->has('field') ? $request->input('field') : 'Random';
$yourValue2 = $request->has('country') ? $request->input('country') : 'Australia';

It's a mouthful, but allows you to write each value out on its own line.

Your $request variable still won't show these fields. If you are planning to check for the field multiple times, then you can either assign a value to $request like in Zakaria's answer, or you can set it in a variable that you will check.

In many cases, you want to assign the input to something, in which case you can use the ternary operator directly when you're assigning it.

For example, if we're setting the country field for the current user, we might use:

$user->country = $request->has('country') ? $request->input('country') : 'Unspecified';

Often, it's easier to bypass the problem. Some other options might be:

  • When using an optional select dropdown, you can set the "empty" default value to have a value when it's submitted. For example, in a country dropdown you might start with:

      <select name="country">
        <option value="unspecified" selected="selected"></option>
        <option>Australia</option>
        <option>Brazil</option>
        <option>Canada</option>
      </select>
    
  • Set fields as required in the form. And verify that a value was submitted using request validation.

      $this->validate(request(), [
        'field' => 'required',
        'country' => 'required:min:3',
      ]);
    

That will return to the form with an $errors collection that can be used to display a message to the user. Put this first, and by the time you need to use the input, the validation has already checked that they submitted something.

like image 191
Azrael Avatar answered Oct 27 '25 14:10

Azrael


The function input not updating the $request it will just check if the given attribute is not present in Request if so it will return default value passed in second argument, try to use has() to check if the attribute is present in request and then update the request manually :

if( $request->has('field')) {
    $request->field = 'Random';
}

Note : The has method returns true if the value is present and is not an empty string.

Hope this helps.

like image 44
Zakaria Acharki Avatar answered Oct 27 '25 13:10

Zakaria Acharki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!