1 year ago

How to stop eager loading when model already have $with

Posted 1 year ago by oroalej


How can i overwrite the $with in the model without removing it? I found setEagerLoads and it is working great. However, when I tried it to other relationship, it will not work.

select('id', 'name', 'slug', 'professional_category_id')
  ->withCount(['reviews', 'connections', 'ratings'])
  ->NoEagerLoads()       <- this is working, the scope is the same with the address.php
    'address' => function($query) {
      $query->select('city_id', 'city_name', 'state_name', 'addressable_id', 'addressable_type')


  protected $with = [

  public function scopeNoEagerLoads($query) {
      return $query->setEagerLoads([]);

Please sign in or create an account to participate in this conversation.