mamreezaa's avatar

Return a model with dynamic eager loading

I have the following table structure.

  1. sellers has many warehouses
  2. warehouses has many phone numbers
  3. warehouses has many emails

altogether 4 models (sellers, warehouse, emails, phones)

Ex:

  1. https://api.local/v1/sellers => this returns all the sellers (without warehouse)
  2. https://api.local/v1/sellers?include[seller]=warehouse (this returns all sellers with warehouses, because i am setting $this->with=['warehouse'])

In here, how can i eager load email, phone models dynamically ? (i only need to load them when there is a request like this) https://api.local/v1/sellers?include[seller]=warehouse&&include[warehouse]=email,phone

can someone help me.

Thanks

0 likes
3 replies
mamreezaa's avatar

Mmmm.

i can get like this

return $this->with('warehouse.email', 'warehouse.phone')->paginate(env('PAGINATION_LIMIT', 100));

m7vm7v's avatar

I would highly recommend you not to use the env() directly in your code -> better if you store this in your configuration files and use the config(). Other than that eager loading as you have found out comes with the ->with().

Please or to participate in this conversation.