The "Easy Laravel Book" website has posted a new tutorial today looking at the use of scopes in Laravel, a packaging method for creating reusable chunks of code for use across the application.

Applying conditions to queries gives you to power to retrieve and present filtered data in every imaginable manner. Some of these conditions will be used more than others, and Laravel provides a solution for cleanly packaging these conditions into easily readable and reusable statements, known as a scope. In this tutorial I’ll show you how to easily integrate scopes into your Laravel models.

He starts with a simple example of a "where" clause made into a method having a name starting with "scope". This is a hard-coded scope but he also shows an example of the other option, dynamic scopes, allowing for input from the user as a part of the execution. He also shows a quick example of using these same scopes with relations, making them a part of the "find" result chain.

Jamie Rumbelow has proposed an idea to help simply some of the interactions your CodeIgniter application can have with your databases by using named scopes.

Named scopes are a really powerful feature of models - they allow you to define a clean, concise syntax when performing queries within your models - and best of all, are really easy to utilize in CodeIgniter. The main principle of a named scope is that you create a method that, combined with method chaining, allows you to add details to your query (generally additional WHERE clauses).

He shows two "before and after" examples of multi-line requests condensed down by making a custom model layer with methods containing the commonly used portions of the database calls and returning the "$this" object so it can be used for chaining.