This can prove helpful for small projects and APIs, but, chances are high that you’ll require controllers for most of your projects. That’s okay; Laravel can do that, too!

001 Route::get(‘orders’, ‘OrdersController@ index’);

Done. Notice how Laravel grows with your needs? This level of accommodation is what makes the framework as popular as it is today.

3. Easy Relationships

What do we do in the instances when we must define relationships? For example, a task will surely belong to a user. How might we represent that in Laravel? Well, assuming that the necessary database tables are set up, we only need to tweak the related Eloquent models.

And, with that, we’re done! Let’s grab all tasks for the user with an id of 1. We can do that in two lines of code.

001 $user = User::find(1);
002 $tasks = $user->tasks;

However, because we’ve defined the relationship from both ends, if we instead want to fetch the user associated with a task, we can do that too.

001 $task = Task::find(1);
002 $user = $task->user;

4. Form Model Binding

Often, it can be helpful to link a form to a model. The obvious example of this is when you wish to edit some record in your database. With form model binding, we can instantly populate the form fields with the values from the associated table row. Because the form in the code below is now linked to a specific ‘Order’ instance, the inputs will display the correct values from the table.

5. Cache Database Queries

If you have too many database queries, your application can very quickly become like molasses. Luckily, Laravel offers a simple mechanism for caching these queries, using nothing more than a single method call.

Let’s grab all ‘questions’ from the database, but cache the query, since it’s not particularly likely that this table will be updated all that frequently.

001 $questions = Question::remember(60)->get();

That’s it! Now, for the next hour of incoming page requests, that query will remain cached, and the database will not even be touched.

6. View Composers

You’ll encounter situations when multiple views require a certain variable or piece of data. A good example of this is a navigation bar that displays a list of tags.

To keep controllers as minimal as possible, Laravel offers view composers to manage things like this. With this piece of code, any time that the ‘layouts/nav.blade.php’ view is loaded, it will have access to a variable, ‘$tags’, equal to the provided array.

7. Simple Authentication

Laravel takes a very simple approach to authentication. Simply pass an array of credentials, likely fetched from a login form, to `Auth::attempt()`. If the provided values match what is stored in the `users` table, the user will instantly be logged in.

8. Resources

Working RESTfully in Laravel has never been easier. To register a resourceful controller, simply call ‘Route::resource()’, as shown on the following page. 001 Route::resource(‘orders’, ‘OrdersController’);

Further, the companion controller may also be generated from the command line:

001 php artisan controller:make OrdersController

Within this generated controller, each method will correspond to one of the routes above. For example, ‘/orders’ will map to the ‘index’ method, ‘/orders/create’ will map to ‘create’, etc. We now have the necessary power to build RESTful applications and APIs with ease.

9. Blade Templating

While, yes, PHP is by nature a templating language, it hasn’t evolved to become an overly good one. That’s okay, though; Laravel offers its Blade engine to fill the gap. Simply name your views with a ‘.blade.php’ extension, and they will automatically be parsed, accordingly. Now, we can do such things as this:

10. Testing Facilities

Because Laravel makes use of Composer, we instantly have PHPUnit support in the framework out of the box. Install the framework and run ‘phpunit’ from the command line to test it out. Even better, though, Laravel offers a number of test helpers for the most common types of functional tests. Here we’re going to verify that the homepage returns a status code of 200.