Add/append values to request array in Laravel
I have encountered a situation where I needed to change the value of a particular request or append values to request array in Laravel before calling an Eloquent method in a store() or update() method with Request parameter. So I did search for how to do that and I will like to share my findings with you. Apparently, pretty easy.
Let’s look at the code for creating employee:
public function store(Request $request) { $newEmployee = $request->all(); // some additional logic or checking Employee::create($newEmployee); }
Changing the Date Format
The code above will create a new employee with the values entered in the form input fields. But what if you want to change the format of the date of employment input from the form. It will look like this:
public function store(Request $request) { $newEmployee = $request->all(); $newEmployee['date_of_employment'] = date('Y/m/d', strtotime($request->date_of_employment)); Employee::create($newEmployee); }
Great? The general logic is to assign an array of key => value to the $request->request property.
Small tip, but I hope it is helpful!
How to Securely Set Laravel 5 Files Folders Permission and Ownership Setup
No Comments
Leave a comment Cancel