More helper functions in Laravel 5.2
This is a series of posts that showcase new features and aha moments in Laravel 5.2
1. Implicit Route Model Binding
2. Simplified Eloquent Global Scopes
3. Append Scheduled Tasks Output
8. More helper functions
9. Fluent Routing
There are Facades and there are helper functions.
Several Facades have helper function equivalents e.g
Response Facade has response()
Collection Facade has collection()
I am a sucker for helper functions because they are very handy to use.
Here is a list of new helper functions that came bundled with Laravel 5.2:
1. validator()
The validator function creates a new Validator instance.
Remember the Validator Facade? With it, it’s like this:
1 2 3 4 5 6 7 8 9 |
protected function store(Request $request) { return Validator::make($request->all(), [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|confirmed|min:6', ]); } |
..but with the validator helper function, we can perform a validation like so:
1 2 3 4 5 6 7 8 9 10 |
protected function store(Request $request) { $rules = [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|confirmed|min:6', ]; validator($request->all(), $rules); } |
You can also pass custom messages and attributes to it because this is the signature for the validator function in Laravel source code:
1 2 3 4 5 6 7 8 9 10 |
/** * Create a new Validator instance. * * @param array $data * @param array $rules * @param array $messages * @param array $customAttributes * @return IlluminateContractsValidationValidator */ function validator(array $data = [], array $rules = [], array $messages = [], array $customAttributes = []){ ... } |
2. resource_path()
The resource_path function returns the path to the resources folder.
3. dispatch()
The dispatch function pushes a new job onto the Laravel job queue.
I can do something like so:
1 |
dispatch(new AppJobsCleanDB); |
where CleanDB
is a Job Class that performs some operations to clean the db after some period.
Note: I’ll keep updating this post as new helper functions are added in Laravel 5.2
Please, if you have any questions or observations, let me know in the comments section below

- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016