Event Listeners

Submitted by bwsewell - 10 years ago

Forget cache upon model events or custom events

// app/global.php
require app_path().'/events.php';

// app/events.php
Event::listen('student.change', function() {
    Cache::forget('query.student.all');
})

// app/models/Student.php
class Student extends Eloquent {
    protected $guarded = [];
    
    public static function boot()
    {
        static::saving(function()
        {
            Event::fire('student.change');
        });
    }
}

// app/controllers/StudentController.php
class StudentsController extends BaseController {
    public function index()
    {
        return Student::remember(10, 'query.student.all')->get();
    }
}