Skip to content

Instantly share code, notes, and snippets.

@ryanmr
Created February 4, 2016 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanmr/5f94a65ac216ee7b7c47 to your computer and use it in GitHub Desktop.
Save ryanmr/5f94a65ac216ee7b7c47 to your computer and use it in GitHub Desktop.
array:2 [▼
0 => array:3 [▼
"query" => "select * from `episodes` where (`hidden` = ? and `unlisted` = ? and `state` = ? and exists (select * from `series` where `episodes`.`series_id` = `series`.`id` and `slug` = ?) or exists (select * from `episodes_relations` as `self_a536d4a0691c58055cceecc94fd32e35` where `self_a536d4a0691c58055cceecc94fd32e35`.`episode_id` = `episodes`.`id` and `type` = ? and exists (select * from `series` where `episodes`.`series_id` = `series`.`id` and `slug` = ?) and `episodes`.`deleted_at` is null)) and `episodes`.`deleted_at` is null order by `published_at` desc, `created_at` desc"
"bindings" => array:6 [▼
0 => false
1 => false
2 => "published"
3 => "pk"
4 => "parent"
5 => "pk"
]
"time" => 7.3
]
1 => array:3 [▼
"query" => "select `episodes`.*, `episodes_relations`.`episode_id` as `pivot_episode_id`, `episodes_relations`.`episode_related_id` as `pivot_episode_related_id`, `episodes_relations`.`type` as `pivot_type` from `episodes` inner join `episodes_relations` on `episodes`.`id` = `episodes_relations`.`episode_related_id` where `episodes_relations`.`episode_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) and `episodes`.`deleted_at` is null"
"bindings" => array:17 [▼
0 => 813
1 => 809
2 => 797
3 => 795
4 => 788
5 => 778
6 => 774
7 => 773
8 => 763
9 => 759
10 => 755
11 => 753
12 => 751
13 => 747
14 => 739
15 => 737
16 => 729
]
"time" => 0.89
]
]
<?php
/*
Goal: get all episodes in the `pk` series slug, and all episodes that have a related parent that is in the `pk` series slug.
*/
$episodes = App\Episode::with(['related'])
->visible()
->recent()
->withSlug('pk')
->orWhereHas('related', function($query){
$query->where('type', '=', 'parent')
->whereHas('series', function($query){
$query->withSlug('pk');
});
})
->get();
/*
The test data seems to be working since there are IDs in a subquery being included with a whereIn I guess.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment