79709110

Date: 2025-07-21 13:09:01
Score: 0.5
Natty:
Report link

Oh yeah. I got it. I'll post the answer in case it's useful to someone. I tried to use merge() earlier, but I should have used union().

In the models we do:

public function myRelation_1 {
  return $this->myRelation()->where('level', 1);
}

public function myRelation_2 {
  return $this->myRelation()->where('level', 2);
}

// Add
public function all_relation() {
  return $this->myRelation_1()->union($this->myRelation_2());
}

// When calling in the controller, pass the following to the with() method
$res = Model::select('id','name', 'price')
->with('all_relation')
->where('status', '=', 1)
->first();

PS: Maybe it will be useful to someone. Thank you all for participating.

Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Studlara