1

So I am trying to create a global scope in laravel 5.1 but just keep on getting the following error Trait 'Eloquent\Scopes\DependentTypeTrait' not found.

Here is my code:

App\Models\Eloquent\Scopes\DependentTypeTrait.php:

<?php namespace App\Models\Eloquent\Scopes;

trait DependentTypeTrait {

    /**
     * Boot the Active Events trait for a model.
     *
     * @return void
     */
    public static function bootDependentTypeTrait()
    {
        static::addGlobalScope(new DependentTypeScope);
    }

}

App\Models\Eloquent\Scopes\DependentTypeScope.php:

<?php namespace App\Models\Eloquent\Scopes;

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

class DependentTypeScope implements ScopeInterface
{

    public function apply(Builder $builder)
    {
        $builder->where('vip_type_id', 2);
    }

    public function remove(Builder $builder)
    {

        $query = $builder->getQuery();

        // here you remove the where close to allow developer load
        // without your global scope condition

        foreach ((array) $query->wheres as $key => $where) {

            if ($where['column'] == 'vip_type_id') {

                unset($query->wheres[$key]);

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

App\Models\Dependent.php:

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Eloquent\Scopes\DependentTypeTrait;

class Dependent extends Model
{

    use DependentTypeTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = '<my table name>';

    /**
     * The actual primary key for the model.
     *
     * @var string
     */
    protected $primaryKey = '<my primary key>';

}

I feel like I have the namespacing correct but its still saying it can't find the attribute....any ideas?

1 Answer 1

1

I think you have the use namespace wrong when trying to import your DependentTypeTrait:

<?php namespace App\Models\Eloquent\Scopes;

trait DependentTypeTrait { //...

And then you use it in App\Models\Dependent:

use Eloquent\Scopes\DependentTypeTrait;

Those namespaces don't match. Try this instead:

use App\Models\Eloquent\Scopes\DependentTypeTrait;

Or you can modify your composer.json to change how the Eloquent namespace is autoloaded:

"autoload": {
    "psr-4": {
        "Eloquent\\": "app/Models/Eloquent"
    }
},

And don't forget to dump your autoloader: $ composer dump-autoload

Though specifically because you're using the name Eloquent which is ubiquitous in Laravel, I wouldn't go this route myself even if you would save yourself some typing. Laravel itself uses the namespace Illuminate\Database\Eloquent so you're probably safe from conflicts for now, but the possibility exists if Taylor changes things around or you use a third party library that stomps on it.

Sign up to request clarification or add additional context in comments.

1 Comment

You are correct! I was assuming that since the namespace on the model was App\Namespace that I didn't have to add this in on the use....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.