Help Needed for Nested Eager Loading Constraints

Solved Posted In Laravel
  • keshan-sandeepa 2 months ago
    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](http://)
    

    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

  • Best Answer

    (As selected by keshan-sandeepa)

    keshan-sandeepa 2 months ago
    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'];
     

    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'];
    
  • keshan-sandeepa 2 months ago
    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'];
     

    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 2 months ago
    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();

    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();

Please login or signup to leave a response.

Login

or Sign Up

Sign Up

or Login
×
Want to create your own SAAS?

I can teach you to create your own Software as a Service. Click here to learn more!

Visit SAAS Adventure!