mercredi 27 janvier 2016

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'); http://ift.tt/1nndB44

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire