Disable eager relations

In my project I have many Eloquent models that have eager relations configured in class like this:

protected $with = [ 'countries', 'roles' ];

But sometimes I need just old plain model without any relations. Can I somehow do:

Model::noRelations()->all()

Really don't wanna use query builder nor create another class just for few occasions.

Answers


If you have to set the $with property on your model rather than leaving it empty, you can manually override the relationships that need to be eager loaded like this:

Model::setEagerLoads([])->get();

Link to API for setEagerLoads


In addition to Thomas Kim answer.

If you anyway extend Eloquent\Model class and often need to strip off relations from model, this solution might suit you well.

  1. Create scope in your default model class:

    public function scopeNoEagerLoads($query){
        return $query->setEagerLoads([]);
    }
    
  2. For any ORM, that extends that class you will be able to:

    User::noEagerLoads()->all()
    

Need Your Help

What are Covering Indexes and Covered Queries in SQL Server?

sql sql-server indexing

Can you explain the concepts of, and relationship between, Covering Indexes and Covered Queries in Microsoft's SQL Server?

How do I declare and use variables in PL/SQL like I do in T-SQL?

sql-server oracle tsql plsql oracle-sqldeveloper

In Sql Server, often times when I'm testing the body of a stored procedure, I copy the body into SSMS, DECLARE the variables at the top of the page, set them to some sample values, and execute the ...