1 year ago

Accessing multiple relationships at once?

Posted 1 year ago by brysonreece

Hey there!

I've come to know that with a table layout like

   - id
   - id
   - meter_id
   - id
   - meter_id
  - id
  - meter_id

I can setup a hasMany Meter->Sensor relationship, as well as a belongsTo Sensor->Monitor. Using this, I know I can call $meter->a_sensors or $meter->b_sensors to get a relationship instance or collection of a specific sensors type.

My question is, I was wondering if it's possible to setup a relationship with this table format so that I may ALSO be able to call $meter->sensors to get a collection instance of all sensors, as well as $meter->sensors() to retrieve a query builder instance.

Bonus question:

If the above is possible, assume I also have the table

  - id
  - sensor_id
  - sensor_type

Would it also be possible to call $meter->sensor_entries (as well as the () builder notation) and get the sensor entries associated with all meter sensors?


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