Skip to content

Instantly share code, notes, and snippets.

@bwsewell
Created December 21, 2013 16:26
Show Gist options
  • Save bwsewell/8071606 to your computer and use it in GitHub Desktop.
Save bwsewell/8071606 to your computer and use it in GitHub Desktop.
Event Listeners in Laravel
<?php
// 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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment