Laravel's laravel_through_key and using Laravel Unions

September 18, 2024

The laravel_through_key

There laravel_through_key is something I've recently stumbled across with all of this new Eloquent work I have been doing and would like to dive deeper into. So far I have found that it has to do with the HasManyThrough relationship

MySQL UNION and UNION ALL

Update on my feature struggles from yesterday. The current solution I am going with is to utilize Laravel Unions in order to grab all of my data from the different tables into a single query, allowing me to filter them, order them and apply pagination. Due to the nature of the data I need to pull, I needed to opt for using UNION ALL since I need to preserve duplicates.

Unfortunately I don't think I can openly share all of the code but here is a super vague overview of what is happening.

$groupMemberPayments = GroupMemberPayment::select(["<target_columns>"])  
    ->join("<target_join_tables>")
    ->distinct()  
    ->where();  

$payments = Payment::select(["<target_columns>"])  
    ->join("<target_join_tables>")  
    ->where("<where_conditions>")  
    ->with("<target_eager_load_relationships>")  
    ->unionAll($groupMemberPayments)  
    ->orderBy("timestamp", "DESC")  
    ->get();

© 2024 Terrence Eisenhower. All rights reserved.