@@ -688,4 +688,42 @@ public function entries_are_found_using_offset()
688688 $ this ->assertCount (2 , $ entries );
689689 $ this ->assertEquals (['Post 2 ' , 'Post 3 ' ], $ entries ->map ->title ->all ());
690690 }
691+
692+ /** @test */
693+ public function entries_can_be_retrieved_on_join_table_conditions ()
694+ {
695+ Collection::make ('posts ' )->save ();
696+ EntryFactory::id ('1 ' )->slug ('post-1 ' )->collection ('posts ' )->data (['title ' => 'Post 1 ' , 'author ' => 'John Doe ' , 'location ' => 4 ])->create ();
697+ EntryFactory::id ('2 ' )->slug ('post-2 ' )->collection ('posts ' )->data (['title ' => 'Post 2 ' , 'author ' => 'John Doe ' ])->create ();
698+ EntryFactory::id ('3 ' )->slug ('post-3 ' )->collection ('posts ' )->data (['title ' => 'Post 3 ' , 'author ' => 'John Doe ' , 'location ' => 4 ])->create ();
699+ Collection::make ('locations ' )->save ();
700+
701+ $ locations = [
702+ 4 => ['slug ' => 'shaldon ' , 'title ' => 'Shaldon ' ],
703+ 5 => ['slug ' => 'cambridge ' , 'title ' => 'Cambridge ' ],
704+ 6 => ['slug ' => 'london ' , 'title ' => 'London ' ],
705+ ];
706+
707+ foreach (range (4 , 6 ) as $ index ) {
708+ EntryFactory::id ($ index )->slug ($ locations [$ index ]['slug ' ])->collection ('locations ' )
709+ ->data (['title ' => $ locations [$ index ]['title ' ]])->create ();
710+ }
711+
712+ $ query = Entry::query ()
713+ ->join ('entries as e ' , fn ($ join ) => $ join
714+ ->whereColumn ('e.id ' , 'entries.id ' )
715+ ->where ('e.collection ' , 'posts ' )
716+ )->leftJoin ('entries as locations ' , function ($ join ) {
717+ $ join
718+ ->where ('locations.collection ' , 'locations ' )
719+ ->on ('locations.id ' , 'e.data->location ' );
720+ })
721+ ->where ('e.data->title ' , 'like ' , '%post% ' )
722+ ->where ('locations.slug ' , 'shaldon ' );
723+
724+ $ entries = $ query ->get ();
725+
726+ // successfully retrieved 2 results
727+ $ this ->assertCount (2 , $ entries );
728+ }
691729}
0 commit comments