06.10.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Eloquent: Getting Started 573<br />

18 }<br />

Now, you may pass the parameters when calling the scope:<br />

1 $users = App\User::ofType('admin')->get();<br />

Events<br />

Eloquent models fire several events, allowing you to hook into various points in the model’s lifecycle<br />

using the following methods: creating, created, updating, updated, saving, saved, deleting,<br />

deleted, restoring, restored. Events allow you to easily execute code each time a specific model<br />

class is saved or updated in the database.<br />

Basic Usage<br />

Whenever a new model is saved for the first time, the creating and created events will fire. If a<br />

model already existed in the database and the save method is called, the updating / updated events<br />

will fire. However, in both cases, the saving / saved events will fire.<br />

For example, let’s define an Eloquent event listener in a service provider. Within our event listener,<br />

we will call the isValid method on the given model, and return false if the model is not valid.<br />

Returning false from an Eloquent event listener will cancel the save / update operation:<br />

1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!