0

I have an Eloquent model called Vegetable with a global scope defined. The model is a subclass of another model I have created called Food that uses the same database table, and the global scope just limits the Vegetable class to working with foods that are vegetables. But the global scope defined in Vegetable seems to be applied to Food when Food is used in Vegetable's method. Is that expected behavior? If so, is there a way around it besides using withoutGlobalScope() when necessary?

class Vegetable extends Food
{
    ...some code omitted

    public function someMethod()
    {
        // this can't find the food unless it's a vegetable
        $food = Food::find(1);
    }

    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope(new VegetableScope);
    }
}
regularmike
  • 1,167
  • 10
  • 22
  • Are you still looking for an answer? – Jonas Staudenmeir Jun 15 '18 at 17:51
  • @JonasStaudenmeir I never did find an answer or bother trying to dig into the framework code. I started avoiding using superclasses like this by just putting that logic in a separate class, which is easy enough. This doesn't come up a lot, but I thought it was odd behavior. – regularmike Jun 15 '18 at 23:36
  • Your code would work with `Food::query()->find(1);`. This is the explanation: https://stackoverflow.com/a/6181737/4848587 – Jonas Staudenmeir Jun 15 '18 at 23:49
  • Ah, right. I wasn't thinking about it in terms of a static binding issue. Makes sense now, sort of. Thanks. – regularmike Jun 16 '18 at 13:42
  • I'm voting to close this as a duplicate of [PHP is handling incorrectly my static call](https://stackoverflow.com/questions/6181603/php-is-handling-incorrectly-my-static-call) – Sᴀᴍ Onᴇᴌᴀ Mar 07 '20 at 00:41

0 Answers0