---
title: "Matteo Orefice (@MatteoOreficeIT) | Hashnode"
description: "Software Developer. Read the latest articles by Matteo Orefice on Hashnode."
url: "https://hashnode.com/@MatteoOreficeIT"
---

@MatteoOreficeIT

# Matteo Orefice

@MatteoOreficeIT · Turin, Italy · Joined March 2018

Software Developer

## Comments

[When you need to call this kind of shared protected methods from the same class boot method, for example to be reused in a global scope an error occurs like this : Non-static method App\\Product::withCalculatedPricingScope() should not be called statically but you can avoid it with this useful trick if ( ! function\_exists('outer\_scope')) { class guestClass23c7d2389d6839b0dcd53a8230d04b35{}; /\*\* \* Easily changes closure scope to an extraneous \* \* @param $closure \* @param null $thisPointer \* @param string $scope \* @return mixed \*/ function outer\_scope($closure,$thisPointer=null,$scope=\\guestClass23c7d2389d6839b0dcd53a8230d04b35::class){ return $closure->bindTo($thisPointer,$scope); } } // Reuse as global scope class Product { // .... protected static function boot() { parent::boot(); // TODO: Change the autogenerated stub self::addGlobalScope(outer\_scope(function($query){ Product::withCalculatedPricingScope($query); })); } // .... // Our actual scope, which will continue to work in exactly the same way as before public scopeWithCalculatedPricing( $query, $include\_tax = false ) { $this->withCalculatedPricingScope( $query, $include\_tax ); } protected function withCalculatedPricingScope( $query, $include\_tax = false ) { $tax\_multiplier = $include\_tax ? 1.2 : 1; $query->selectRaw( 'products.\*, ( products.price \* ' . $tax\_multiplier . ' ) as total\_price' ) ->where( 'stock', '>', 0 ); } }](https://hashnode.com/posts/a-little-trick-with-eloquent-query-scopes-that-makes-them-super-reusable/5890be485478d2f4f656800e/comment/5ab37d475bb4ae0c73d075bd)

Comment · Article · Mar 22, 2018 · [A little trick with Eloquent query scopes that makes them super reusable](https://hashnode.com/posts/a-little-trick-with-eloquent-query-scopes-that-makes-them-super-reusable/5890be485478d2f4f656800e)

[It would be very but ... in Laravel 5.4 where accepts as first parameter a string or a Closure, and so : (new BeesAlive instanceof \\Closure) != true](https://hashnode.com/posts/a-little-trick-with-eloquent-query-scopes-that-makes-them-super-reusable/5890be485478d2f4f656800e/comment/59bc85fcd692facd74b6f4a3#reply-5ab379d95bb4ae0c73d0759d)

Reply · Article · Mar 22, 2018 · [A little trick with Eloquent query scopes that makes them super reusable](https://hashnode.com/posts/a-little-trick-with-eloquent-query-scopes-that-makes-them-super-reusable/5890be485478d2f4f656800e)
