Laravel 4.2 introduced a new way of handling soft deletes by making use of traits and query builder macros wrapped in as a feature, that many people asked for for a very long time – Global Scopes.

This might be very useful for everyone, however the docs are not very eloquent in this case. So keep on reading to learn how you can implement global scopes in your project – it’s really easy and lets you write even more expressive code for your Eloquent models.

Check the ready-to-use demo at …

 

https://github.com/jarektkaczyk/laravel4-global-scope-example

 

Below example applies to Laravel 4.2. For Laravel 5 solution available click here.

 

 

Applying global scope requires just 2 simple steps:

  1. Create a class AbcScope that implements ScopeInterface
  2. Boot that class in your Eloquent model calling static::addGlobalScope(new AbcScope)

So basically you could just create the scope and then boot it in the model, which should use the scope.

 

However this is not going to be very reusable, so instead let’s take a bit longer path (and this is how SoftDeleting is implemented in the Eloquent core):

  1. Create the scope
  2. Create a trait that will boot the scope and implement some handy methods
  3. Add single use AbcTrait line to all the models that will use the scope

 

NOTE: Below example assumes no namespaces for the Scope and/or Trait so you can place both in app/models folder which is autoloaded. Linked demo uses namespaces in order to show both ways.

As an example let’s use Post with draft/published feature. By default we would like to get only published posts, but when we are logged in to the admin area, we need to see also the drafts:
(In real life I would use whereNull('published_at') as a constraint, however this would be exactly the same as SoftDeletingTrait, so instead I’m gonna check for published=1 condition, for this is a bit trickier)

 

 

Usage

1. Apply the scope for the Post model:

2. Use it:

 

 

And that’s all!

Be sure to check the SoftDeletingTrait too and play with the global scopes. I’m sure you will love it!

It's only fair to share...Tweet about this on TwitterShare on RedditPin on PinterestShare on FacebookShare on Google+

Related Post

Querying relations in Laravel: get Models where la... Another part of Querying relations in Laravel will cover such problem: I want to get SomeModels that have latest RelatedModel (hasMany) matchin...
Tweaking Eloquent relations – how to get lat... Have you ever needed to show only single related model from the hasMany relationship on a set of parents? Being it latest, highest or just rand...
How to store clients in database? That is, Party M... Have you ever been building any app that handled client's data, was it e-commerce, crm or anything similar, you surely wondered, how to store it in ...
Querying relations in Laravel 4: nested relation Previously we dealt with simple relations defined in Eloquent models and eager loading them in order to save database queries. Now imagine nest...