php - How to make Request default in Laravel 5.1 when none is given -
i using laravel 5.1 , need make default value when in field none given.
so 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 have:
you may pass default value second argument input method. value returned if requested input value not present on request:
$name = $request->input('name', 'sally');
so when have in "field" have:
array:21 [▼ "country" => "" "field" => "dddd" "_token" => "xemi" ]
but when "field" empty have
array:21 [▼ "country" => "" "field" => "" "_token" => "xemi" ]
instead of
array:21 [▼ "country" => "" "field" => "random" "_token" => "xemi"
thanks helping me
the function input
not updating $request
check if given attribute not present in request if return default value passed in second argument, try use has()
check if attribute present in request , update request manually :
if( $request->has('field')) { $request->field = 'random'; }
note : has
method returns true
if value present , not empty string.
hope helps.
Comments
Post a Comment