append values to request array

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Attach images – Only PNG, JPG, JPEG and GIF are supported.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Quote of the week

"People ask me what I do in the winter when there's no baseball. I'll tell you what I do. I stare out the window and wait for spring."

~ Rogers Hornsby

All rights resolved