Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Easy Laravel Book:
Using Scopes With Laravel 5
Jun 24, 2015 @ 11:10:45

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.

tagged: laravel5 easylaravelbook scopes tutorial

Link: http://www.easylaravelbook.com/blog/2015/06/23/using-scopes-with-laravel-5/

Jamie Rumbelow's Blog:
Named Scopes with CodeIgniter
Jan 07, 2010 @ 12:20:20

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.

tagged: named scopes codeigniter framework

Link: