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

Writing advanced Eloquent search query filters
Jun 15, 2016 @ 11:10:04

In a new post from the Dotdev.co site there's a tutorial from Amo Chohan helping you write advanced search query filters for Eloquent in your Laravel application.

I recently needed to implement a search feature in an events management project I was building. What begun as a few simple options (searching by name, e-mail etc), turned into a pretty large set of parameters.

Today, I’ll go over the process I went through and how I built a flexible and scalable search system. For those of you who are eager to see the final code, head over to the Git repository to see the code.

He starts off by outlining what he'll be creating and where the need comes from for this more advanced filtering. He uses a company-wide calendar example with events and meetings/clients shown for all users. He defines the filters he knows he'll want to search by and the models relating to the data needed for those queries. He then spends the rest of the post going through the code needed to implement the filtering, starting with a rough (but working) version and refactoring from there. He moves away from the procedural method of applying filters to a query object directly and over to "applying" them more dynamically using a set of filter instances via a Decorator design pattern approach.

tagged: advanced eloquent filter search decorator apply refactor tutorial

Link: https://dotdev.co/writing-advanced-eloquent-search-query-filters-de8b6c2598db#.16sfoe3a8

SitePoint PHP Blog:
Laravel Doctrine – Best of Both Worlds?
Jun 14, 2016 @ 11:56:59

On the SitePoint PHP blog there's a tutorial posted from Francesco Malaesta about the Laravel Doctrine project, wondering if it's the best of both worlds for connecting to databases and working with entities in your Laravel-based project.

Laravel Doctrine is a drop-in implementation of the famous ORM for the Laravel 5.X Framework, and a really interesting alternative to the default choice, Eloquent. In this article, we will learn how to use it, and when.

He starts with a comparison of the two options - Doctrine vs Eloquent - and why you might choose the former over the latter. He then gets into the installation of a new Laravel project and pulling in the laravel-doctrine/orm package. He then shows it in action with a simple to-do list application, creating an entity for the tasks. From there he shows the use of the EntityManager to add a new task and find ones matching certain criteria. He makes a TaskController to handle the route functionality and creates the matching frontend views to allow the user to define new tasks. Also included is the code for the other "CRUD" (create, read, update, delete) handling for the tasks and the addition of the relationship between users and tasks.

tagged: laravel doctrine tutorial eloquent todo list

Link: https://www.sitepoint.com/laravel-doctrine-best-of-both-worlds/

Rob Allen:
Using Eloquent in Slim Framework
Apr 07, 2016 @ 09:45:31

Rob Allen has a quick post showing how to use Eloquent in a Slim framework application to work with your database queries and functionality.

Eloquent is one of the easier ORMs to get started with, so let's look at how to use and test it within a Slim Framework application. [...] Eloquent is quite a nice implementation of the Active Record pattern. If that meets your needs, then it is easy to use within a Slim Framework application, so go for it!

He walks you through the set up of the Eloquent package via a Composer require and update your Slim configuration with the database settings. He then includes the few lines you'll need to use the "capsule" manager to integrate it with the Slim structure. He also talks about testing models and shows how to create some simple tests with PHPUnit and a bit of mocking.

tagged: slimframework eloquent database capsule composer tutorial

Link: https://akrabat.com/using-testing-eloquent-in-slim-framework/

Jeff Madsen:
Eloquent Create, Update, New...the Untold Story 2016-04-01
Apr 06, 2016 @ 12:19:20

Jeff Madsen has a quick post to his site with the "untold story" behind Eloquent's save and create in the Laravel framework.

Over on Laravel Quick Tips we've been looking at a few of these functions and their uses, and I thought it might be helpful to collect all of them together in a single (I hope) coherent post.

Without further preamble, let's get to it. I'm going to use the basic User object and table that ships with a default installation of Laravel so you can follow along if you like.

He starts with the difference between "new" and "create", pointing out the one fundamental difference: one saves, one does not. He then looks at some of the other new/create functions (like findOrNew, firstOrCreate, updateOrCreate), what each of them does in the background and a quick snippet of code showing.

tagged: eloquent laravel create update new behindthescenes difference save

Link: http://codebyjeff.com/blog/2016/04/eloquent-create-update-new-the-untold-story

The Repository Pattern in Laravel 5
Mar 15, 2016 @ 12:50:05

The TutsPlus.com site has posted a tutorial covering the use of the repository design pattern in Laravel, a popular PHP framework. While the article does introduce some of the basics of the design pattern, a bit more research may be in order if you're not familiar with its base concepts.

The repository pattern was introduced for the first time by Eric Evans in his Domain-Driven Design book. The repository is, in fact, the entry point for the application to access the domain layer.

To put it simply, the repository allows all your code to use objects without having to know how the objects are persisted. The repository contains all the knowledge of persistence, including mapping from tables to objects. This provides a more object-oriented view of the persistence layer and makes the mapping code more encapsulated.

In this article they replace the default storage/persistence method for database records away from the default Eloquent over to Doctrine, a widely used ORM tool. The start with a brief comparison between Eloquent and Doctrine before starting on on a simple "blog" application. They connect Doctrine to Laravel via an "entity manager" and make a basic Doctrine-based model for the Posts and a matching repository/controller implementation. All the code you'll need to implement it is included along with a simple view to take in the post content and display errors if there were any on the save.

tagged: repository designpattern laravel tutorial doctrine eloquent blog

Link: http://code.tutsplus.com/tutorials/the-repository-pattern-in-laravel-5--cms-25464

Laravel Podcast:
Episode #42 - Shots Fired
Feb 25, 2016 @ 12:18:28

The Laravel Podcast, hosted by Matt Stauffer (with guest hosts Taylor Otwell and Jeffrey Way) has posted its latest episode - Episode #42: ActiveRecord & The School Of Zonda:

In this episode, the crew is joined by Adam Wathan to discuss ActiveRecord and the Eloquent ORM.

Lots of talk around ActiveRecord vs DataMapper and discussing the positives and negatives to each.

You can listen to this latest episode either through the in-page audio player or by downloading the mp3. If you enjoy the show, be sure to subscribe to their feed or follow them on Twitter of the latest show updates.

tagged: eloquent activerecord mattstauffer ep42 podcast laravel orm

Link: http://www.laravelpodcast.com/episodes/27610-episode-42-activerecord-the-school-of-zonda

Johannes Siipola:
How to use Eloquent ORM migrations outside Laravel
Jan 20, 2016 @ 11:55:10

On his site Johannes Siipola has posted a tutorial showing how to use the Eloquent ORM outside of Laravel, more specifically the migration functionality to manage database table creation/updates/deletions.

Laravel's Eloquent is one of the most fluent and expressive ORM's available for PHP. However, unlike some of its competitors like Doctrine or Propel, it's highly tight to the Laravel ecosystem in some ways. It's still possible to use Eloquent outside Laravel with a bit of work.

[...] If you use Eloquent outside Laravel, you will be missing one of it's best features: database migrations. [...] There has been some effort in order to use Laravel migrations outside the framework, but it hasn't been possible because the migrations require a complete instance of Laravel to run. In this tutorial, we are using framework agnostic Phinx migration library instead.

He then shows how to install the packages needed for the example: Slim framework, the Eloquent ORM and Phinx. He then shows how to set up the database and Phinx configurations and create your first Phinx migration. Inside this migration he makes use of the Eloquent schema builder functionality to create a simple "widgets" table. He ends the post showing you how to create the simple Slim application and work with the model they've created to both save and read data easily.

tagged: eloquent laravel outside orm migration phinx slimframework tutorial

Link: https://siipo.la/blog/how-to-use-eloquent-orm-migrations-outside-laravel

SitePoint PHP Blog:
Modeling an Aggregate with Eloquent
Nov 30, 2015 @ 11:49:19

The SitePoint PHP blog has a tutorial posted from Andrew Cairns showing you how to model an aggregate with Eloquent, the database access layer from the Laravel framework. The "aggregate" here is an implementation of the Aggregate design pattern, a system of smaller opbjects operating as a whole.

The Aggregate pattern is an important part of Domain Driven Design. It prevents inconsistencies and is responsible for enforcing business rules within a collection of objects. For these reasons alone, it is clear to see why it is a key component of a domain model.

[...] Mixing persistence concerns into a Domain Model can become complex and lead to a lot of bad decisions. This does not mean that it is impossible to create an Active Record Domain Model. In this article, we will work through an example of building an Aggregate which also extends Eloquent: a popular Active Record ORM.

They start with a brief summary of what the Aggregate design pattern, how its objects work together and the point of a "root" object. They help illustrate how it would in a more real-world situation with a simple blog system, starting with a simple Post object minus the Eloquent integration. From there they bring in Eloquent, showing how to extend it with the class and what features it introduces. Most of the changes they make then revolve around a "lock" on the post and where value objects and the invariant rule comes in to play.

tagged: aggregate eloquent designpattern domaindrivendesign tutorial

Link: http://www.sitepoint.com/modeling-aggregate-eloquent/

Jon LeMaitre:
Separation of Concerns with Laravel’s Eloquent (Series)
Nov 10, 2015 @ 09:10:47

Jon LeMaitre has written up a series of posts talking about effective separation of concerns using Eloquent, the database access component of the Laravel framework. His goal is to show you how makes use of the library and keep your application both maintainable and readable.

Eloquent (and by extension, Laravel) is often criticized because of its ActiveRecord ORM implementation, and that DataMapper produces a cleaner, more maintainable architecture in the long run. [...] This combination of behaviors crammed into a single object in itself is not the problem (in fact, it’s a wonderful asset), the problem is in how you make use of those behaviors throughout your application. You can be sloppy with them, or you can be tidy with them.

He starts with the "sloppy approach" where different methods of accessing the objects (and their data) are spread all across the application. He counters this with the tidy approach that makes use of "persistence-agnostic 'POPOs'" to hide the logic behind repositories. In the second part of the series he starts in and puts this idea into practice, using a simple "Member" table and model and repository classes to abstract the logic. Finally, in part three he gets into some more extended functionality for the repositories approach: handling collections, relations, eager loading and schema changes.

tagged: laravel eloquent designpattern repository orm datamapper activerecord abstraction tutorial series

Link: https://medium.com/laravel-news/separation-of-concerns-with-laravel-s-eloquent-part-1-an-introduction-c9a6dc6b4a65#.o0qsccqos

Laravel News:
How To Add Multilingual Support to Eloquent
Sep 09, 2015 @ 09:36:52

The Laravel News site has a tutorial they've posted showing you how to help internationalize your application by adding multilingual support to Eloquent in your database schemas.

Did you know that several countries throughout the world have more than one official language? For example, my home country Belgium has three; French, Dutch, and German. Most Belgians also speak English. Laravel’s Eloquent ORM is a very powerful part of Laravel. Unfortunately, it doesn’t provide support for multilingual models out of the box. Adding that functionality yourself isn’t that difficult.

They start with a basic migration with no multilingual support included, a simple "articles" table. They show how support could be included by adding columns for languages but suggests that using a "translated" table for the other versions is a better option. With this schema in place, they then show how to use the laravel-translatable package and Translatable trait to handle the pull and push of the translated data from the database automatically. It can even be set up to pull which version it should display directly from the application's locale setting.

tagged: multilingual support eloquent translatable package locale tutorial

Link: https://laravel-news.com/2015/09/how-to-add-multilingual-support-to-eloquent/