Skip to content

Laravel 10 criteria no longer work?  #799

@DeGraciaMathieu

Description

@DeGraciaMathieu

Hi,

Is anyone using the criteria in Laravel 10 ?

It appears that the criteria are no longer being applied to the model.

This method is located in the BaseRepository class, which is responsible for instantiating a new instance of the model:

public function makeModel()
{
    $model = $this->app->make($this->model());

    if (!$model instanceof Model) {
        throw new RepositoryException("Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model");
    }

    return $this->model = $model;
}

This approach seems unusual because instantiating the model in this way doesn't allow us to apply a where clause.

It's somewhat equivalent to doing this :

app(User::class)->whereNotNull('verified_at')->get();

In this example, app returns an instance of the model, not of EloquentBuilder. I don't understand how this approach still works as intended.

The only solution I've found is to implement the makeModel method in my repository class, which is not ideal :

class UserRepositoryEloquent extends BaseRepository implements UserRepository
{
    public function model()
    {
        return User::class;
    }

    public function makeModel()
    {
        $model = $this->app->make($this->model());

        return $this->model = $model->query();
    }

    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }
}

Does anyone have a better idea?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions