Laravel Magic in Eloquent and View


Today in the #laravel IRC we were talking about some Laravel magic in Eloquent models. It looked very neat and I started looking for other cases where Laravel uses this approach.

What you can do

These are some examples for the Eloquent, View and Redirect class.

Eloquent

The snakecase is taking care of underscore fields.

User::where('first_name', '=', 'simon');
User::whereFirstName('zwacky');

View

Actually the withErrors method is special, as it checks if the passed argument is a MessageBag instance or just an array.

View::make('templates.base')->with('errors' => $errors);
View::make('templates.base')->withErrors($errors);

View::make('templates.dashboard')->with('user' => $user);
View::make('templates.dashboard')->withUser($user);

Dive into the source

I’ll take the source bit for the Eloquent Model example in the Builder class.

All methods that start with ‘where’ will be accepted. The same principle is working in the Eloquent Model but starting with ‘with’.