Laravel 6 Eloquent Global Scope Tutorial Example

Today, We want to share with you Laravel 6 Eloquent Global Scope Tutorial Example.In this post we will show you Lesson 6.2 Global Scopes and Traits, hear for Global Query Scopes And Pagination we will give you demo and example for implement.In this post, we will learn about laravel global scope with parameter with an example.

Laravel 6 Eloquent Global Scope Tutorial Example

There are the Following The simple About Creating and Using Query Scopes with Laravel 6.2 Full Information With Example and source code.

Read Also:  Laravel 6 Deletes method using Eloquent ORM

As I will cover this Post with live Working example to develop Re-using query constraints in Laravel Eloquent, so the laravel accessor date format is used for this example is following below.

Step 1: Applay Eloquent Global Scope


<?php namespace Sofa\Eloquent\Scopes;

use Illuminate\Database\Query\Builder as BaseBuilder;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ScopeInterface;

class MovieUpcommingScope implements ScopeInterface {
	public function apply(Builder $builder, Model $model)
		$column = $model->getQualifiedPublishedColumn();

		$builder->where($column, '=', 1);


	public function remove(Builder $builder, Model $model)
		$query = $builder->getQuery();

		$column = $model->getQualifiedPublishedColumn();

		$bindingKey = 0;

		foreach ((array) $query->wheres as $key => $where)
			if ($this->isPublishedConstraint($where, $column))
				$this->removeWhere($query, $key);
				$this->removeBinding($query, $bindingKey);
			if ( ! in_array($where['type'], ['Null', 'NotNull'])) $bindingKey++;

	protected function removeWhere(BaseBuilder $query, $key)

		$query->wheres = array_values($query->wheres);

	protected function removeBinding(BaseBuilder $query, $key)
		$bindings = $query->getRawBindings()['where'];

	protected function isPublishedConstraint(array $where, $column)
		return ($where['type'] == 'Basic' && $where['column'] == $column && $where['value'] == 1);

	protected function addWithDrafts(Builder $builder)
		$builder->macro('withDrafts', function(Builder $builder)
			$this->remove($builder, $builder->getModel());

			return $builder;

Step 2: Create a Laravel 6 Scope


<?php namespace Sofa\Eloquent\Scopes;

trait MovieUpcommingTrait {

	public static function bootPublishedTrait()
		static::addGlobalScope(new MovieUpcommingScope);

	public function getPublishedColumn()
		return defined('static::PUBLISHED_COLUMN') ? static::PUBLISHED_COLUMN : 'published';

	public function getQualifiedPublishedColumn()
		return $this->getTable().'.'.$this->getPublishedColumn();

	public static function withDrafts()
		return with(new static)->newQueryWithoutScope(new MovieUpcommingScope);

Step 3: Define a Laravel 6 Model

Laravel scope for the Movie model:

<?php namespace App;

use Illuminate\Database\Eloquent\Model as Eloquent;
use Sofa\Eloquent\Scopes\MovieUpcommingTrait;

class Movie extends Eloquent {

    use MovieUpcommingTrait;


Step 4: Get All Records using Laravel 6 Eloquent



App\Movie::where('is_active', 'yes')->withDrafts()->get();

Web Programming Tutorials Example with Demo

Read :

Read Also:  Laravel 6 Inserting & Updating Eloquent Models


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel global scope with parameter.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Show country state city dropdown select box using javascript
  2. Read Also:  How to implement a switch-case statement in Python?
  3. Read Also:  Laravel 6 Eloquent ORM MVC Tutorial
  4. Read Also:  Class HTML not found laravel
  5. Read Also:  Laravel Eloquent Useful Tips and Tricks
  6. Read Also:  How to create model object in controller in laravel?
  7. Read Also:  ASP.NET MVC Web Configuration File web.config
  8. Read Also:  Create a Registration and Login System with PHP and MySQL
  9. Read Also:  Material Design Add icons in Select options
  10. Read Also:  Laravel All Where eloquent methods with Examples