Al-Amin
Al-Amin

Reputation: 798

How to store in existing cache collection and update cache in laravel

return Cache::remember('districts.all',60*60, function (){
    return District::all();
});

cache collection is

[
 {
    "id": 1,
    "title": "Dhaka"
 },
 {
    "id": 2,
    "title": "Rajshahi"
 },
 {
    "id": 3,
    "title": "Barishal"
 }
]

now how to add new in districts.all cache

 "id":4 ,
 "title":"New District"

and also how to update this cache

 "id":3,
 "title":"Khulna"

Upvotes: 0

Views: 3222

Answers (1)

YouneL
YouneL

Reputation: 8361

Laravel doesn't have a special function to update cache directly, but you still can clear districts.all and redefine it again with the new value:

Cache::forget('districts.all');

$district = new District();
$district->id = 4;
$district->name = "New District";

Cache::remember('districts.all', 60*60, function () use ($district) {
    return District::all()->push($district);
});

Upvotes: 1

Related Questions