I am attempting to modify a Laravel Request
object using merge
to update the key trial_end
.
I am doing this using the following code...
if ($this->request->get('trial_end', '')) {
$this->request->merge(array('trial_end' => 'test'));
}
dd($this->request->all(), $this->request->get('trial_end'));
I expect $this->request->get('trial_end')
to be test
, but it is not. $this->request->all()
returns what I expected.
Result of die dump
array:1 [
"trial_end" => "test"
]
"12/4/2018"
How come it is not returning the updated value?
Figured it out. The solution was to change
$this->request->get('trial_end');
to
$this->request->input('trial_end');
This works because input()
adds the data in all()
to getInputSource()->all()
before doing a data_get
on that, whereas get()
just performs a data_get
on the input parameters (pre-modifications).
New code (with a change suggested by Alex)
if ($this->request->has('trial_end')) {
$this->request->merge(['trial_end' => 'test']);
}
dd($this->request->all(), $this->request->input('trial_end'));
New results
array:1 [
"trial_end" => "test"
]
"test"
Hope this helps any others that come across this issue.
the problem it's not with the assignment, but with the comparison, here's my personally best way to check if a value in the request has been set.
public function test(Request $request){
if (!$request->has('trial_end')) { //this is what you have wrong
$request->merge(array('trial_end' => 'test'));
}
return $request->get('trial_end');
}
greetings