Help Needed for Nested Eager Loading Constraints

Solved
keshan-sandeepa

Aug 8th, 2019 05:01 AM

I'm trying to fetch from the database nested query that's not in relationship with current model, In the attachment Division Model have a relationship with results , and results have relationship candidate , i need to fetch the candidate result along with division result , how to do that?? please check the below image : https://drive.google.com/file/d/1vpRW43KHLDesTnE1tCo2xzqss4iXxg0b/view?usp=sharing

keshan-sandeepa

Aug 8th, 2019 07:41 AM

Okay I found some solution , In side the my model i added Eager loading by default , So even i fetch the data from Results its coming with default Relationship
So code look like this (controller). Let me know if you have any other method !

 $news2 =Division::with('results')
        ->with('districts')
        ->where([['result_released',1]])->get();
					
			Result Model
			  protected $with = ['candidates'];
cookie

Aug 11th, 2019 02:14 AM

Best Answer

If you want to refactor your code a little bit, you can use a one-liner.

Division::with(['results', 'districts'])->where(['result_released', true])->get();

Report
1