---
title: "Eric Tucker (@tucker_eric) | Hashnode"
description: "Weather you think you can or you can't, you're right. Read the latest articles by Eric Tucker on Hashnode."
url: "https://hashnode.com/@tucker_eric"
---

@tucker\_eric

# Eric Tucker

@tucker\_eric · Costa Mesa, CA · Joined February 2017

Weather you think you can or you can't, you're right.

## Comments

[Of course! One of the reasons I love Eloquent is because it is very intuitive and there was a lot of work put in behind the scenes to lower that barrier to entry for those new to Laravel or even PHP in general. If I can give advice to anyone working with Eloquent it's that if the steps you're taking to achieve what you're trying to do seem repetitive or unnecessary, they probably are. If something seems too complicated then you're probably over thinking it. 100% of my development knowledge came from the open source community and people like you that take time out of their schedules to write articles and documentation for people to learn. Keep up what you're doing and know that the lurkers are appreciative of your contributions. Right or wrong way, we learn them all to become better at our craft. In fact, I learn more from every mistake I have made and bug I have introduced than I have from copying and pasting code from github. Keep up the writing and feel free to reach out to me for any reason. Cheers!](https://hashnode.com/posts/a-little-trick-with-eloquent-query-scopes-that-makes-them-super-reusable/5890be485478d2f4f656800e/comment/5891cef0cfe48c4f6b113b48#reply-5894cb7ef4fe73f5da76c825)

Reply · Article · Feb 3, 2017 · 2 · [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)

[Using the magic methods, it creates an instance of our model (in case we called it statically), finds the scope instance method, and passes in a new query builder. This is actually very incorrect An instance of the model is ONLY created when you call a method statically. Then in the case that the method you're calling isn't a method of that model and the method is not increment() or decrement() the model assumes you're referrencing the Eloquent Builder, creates a new query instance which sets the model on the Eloquent Builder instance and then finally calls the method that was called on the model statically on the instance of the new query builder. But for our subquery to work properly and benefit fully from the scope, we need the scope function to know about our current $query, which includes everything about the parent query. This is flat out false because the Customer query has already been run and returned before the products query is even called. with() just accessed the builder's with() method which in turn adds it to the Builder's protected $eaderLoad property which doesn't get referenced to query the relations until AFTER the first query is ran in the get() method because that relation is going to need to primary keys returned from the first query to run the second. In the example provided below, $query is an instance of the Eloqent Builder with reference to the Product model so $query->withCalculatedPricing(true) absolutely works because as I stated above, the withCalculatedPricing() method does not appear on the Builder itself so in turn it checks for a macro, which doesn't exist. Then it checks to see if Product has a method called scopeWithCalculatedPricing and calls that method with 2 parameters passing an instance of itself as the first parameter and true to the second and returns the result which why it is IMPERATIVE you return the query from the scope on the model when you write it, so you are passing back the same instance of the query $customers = Customer::with( \[ 'products' => function ( $query ) { // $query in here is the \*only\* thing that has all of the stuff we need to build the correct query }\]); This is only available from the variable we define as the first parameter of our callback function, $query. Somehow, we need to pass this into our scope. This is not true at all because when a method doesn't exist on the Builder itself, first the builder prepends the array of parameters passed to it with itself which makes it the first argument passed to any function called from this magic method. Then the builder checks if a macro exists and when it doesn't then the builder checks for a scope on the model and calls it if it exists. @dbw is correct in stating you need to return the query. For your example, the correct way would be: public function scopeWithCalculatedPricing( $query, $include\_tax = false ) { $tax\_multiplier = $include\_tax ? 1.2 : 1; return $query->selectRaw( 'products.\*, ( products.price \* ' . $tax\_multiplier . ' ) as total\_price' ) ->where( 'stock', '>', 0 ); } With the above you can do: $customers = Customer::with( \[ 'products' => function( $query ) { // You are in the Product query builder which has access to all local scopes on Product $query->withCalculatedPricing(true); }\]); Source: I am the author of EloquentFilter](https://hashnode.com/posts/a-little-trick-with-eloquent-query-scopes-that-makes-them-super-reusable/5890be485478d2f4f656800e/comment/5891cef0cfe48c4f6b113b48#reply-5893bab40908478ecef3c1b1)

Reply · Article · Feb 2, 2017 · 1 · [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)
