Laravel whereHas not working in newQuery()

2019-08-01 15:46发布

I'm having some troubles getting a piece of code to work in laravel. I have a SecuredEloquent class that all my 'secured' models extend. what it does is simply add a whereHas clause to the query in the newQuery function I override:

class SecuredEloquent extends Eloquent
{
    public function newQuery($excludeDeleted = true)
    {
        $query = parent::newQuery($excludeDeleted);

        $context = App::make('Wall\Context\Context');

        $query->whereHas('permissions', function($q) use ($context)
        {
            $q->where('context_id','=',$context->id());
            $q->where('level','>=', $context->level());
        });

        return $query;
    }
}

The problem: it doesn't work. I get "The connection was reset" errors in my browser and nothing in the log :( anyone any idea what I'm doing wrong?

edit2: MyModel extends SecuredEloquent, OtherModel doesn't

When I try to run it in artisan tinker nothing happens. var_dump(MyModel::all()); simply stops the process (it crashes? no idea, again nothing logged, it simply quits) var_dump(OtherModel::all()); however simply works

edit:

I have ContextServiceProvider:

use Illuminate\Support\ServiceProvider;

class ContextServiceProvider extends ServiceProvider
{
    /**
     * Register
     */
    public function register()
    {
        $this->app->singleton('Wall\Context\Context', function($app)
        {
            return new AppContext;
        });
    }
}

with AppContext:

use Illuminate\Database\Eloquent\Model;

class AppContext
{

    /**
     * The current context
     *
     * @var Illuminate\Database\Eloquent\Model
     */
    protected $context;

    /**
     * The current context level
     *
     * @var int
     */
    protected $level;

    /**
     * Check to see if the context has been set
     *
     * @return boolean
     */
    public function has()
    {
        if($this->context) return true;

        return false;
    }

    /**
     * Get the context identifier
     *
     * @return integer
     */
    public function id()
    {
        if ( $this->has() ) return $this->context->id;
        return 0;
    }

    /**
     * Get the context permission leven
     *
     * @return string
     */
    public function level()
    {
        if ( $this->level ) return $this->level;
        return 1;
    }

}

Hope this helps

0条回答
登录 后发表回答