The new cache() global helper in Laravel 5.3
This is a series of posts on New Features in Laravel 5.3.
!Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.
Time for another new feature in Laravel 5.3!
Shortcut global helpers in Laravel
As I was writing my book I noticed a pattern in the global helper functions like session()
and, in some ways, cookie()
. There are three primary functions that they can perform: get
a value, put
a value, or return an instance of their backing service.
For example:
session('abc', null)
gets the value ofabc
, or an optional fallback ofnull
.session(['abc' => 'def'])
sets the value ofabc
todef
.session()
returns an instance of theSessionManager
.
The third option means you can use session()->all()
(or any other methods) just like you would Session::all()
.
I mentioned that it seems like there should be a cache()
helper, and before I could even think much more about it, Jeffrey (Way) had already written one up. So! Behold! The global cache()
helper, new in Laravel 5.3.
The cache()
global helper
Like session()
, the cache()
global helper can perform three primary functions: get
, put
, or return an instance of the backing service.
For example:
cache('abc', null)
gets the cached value ofabc
, or an optional fallback ofnull
.cache(['abc' => 'def'], 5)
sets the value ofabc
todef
, for the duration of5
minutes.cache()
returns an instance of theCacheManager
.
The third option means you can use cache()->forever()
(or any other methods) just like you would Cache::forever()
.
That's it! Enjoy!
Comments? I'm @stauffermatt on Twitter
Tags: laravel • laravel 5.3
This is part of a series of posts on New Features in Laravel 5.3:
-
Jun 16, 2016 | laravel, laravel 5.3, echo, websockets
-
Jun 27, 2016 | laravel, laravel 5.3
-
Jun 29, 2016 | laravel, laravel 5.3, eloquent
-
Jul 6, 2016 | laravel, laravel 5.3
-
Jul 8, 2016 | laravel, laravel 5.3
-
Jul 8, 2016 | laravel, laravel 5.3, eloquent
-
Jul 25, 2016 | laravel, laravel 5.3
-
Jul 26, 2016 | laravel, laravel 5.3
-
Jul 27, 2016 | laravel, laravel 5.3, routing
-
Jul 27, 2016 | laravel, laravel 5.3, laravel scout, laravel passport, mailable
-
Jul 29, 2016 | laravel, laravel 5.3, laravel scout
-
Jul 30, 2016 | laravel, laravel 5.3, laravel passport, oauth
-
Aug 5, 2016 | laravel, laravel 5.3, mail, laravel mailables
-
Aug 8, 2016 | laravel, laravel 5.3
-
Oct 19, 2016 | laravel, laravel 5.3, laravel notifications, notifications
-
Dec 21, 2016 | laravel, laravel 5.3, vuejs, vueify, authorization
-
Dec 21, 2016 | laravel, laravel 5.3, queues
-
Jan 30, 2017 | laravel, laravel 5.3, artisan