diff --git a/src/Entries/EntryQueryBuilder.php b/src/Entries/EntryQueryBuilder.php index 8e94f2b0..67b909f6 100644 --- a/src/Entries/EntryQueryBuilder.php +++ b/src/Entries/EntryQueryBuilder.php @@ -33,6 +33,9 @@ protected function column($column) return $column; } + $table = Str::contains($column, '.') ? Str::before($column, '.') : ''; + $column = Str::after($column, '.'); + if ($column == 'origin') { $column = 'origin_id'; } @@ -43,7 +46,7 @@ protected function column($column) } } - return $column; + return ($table ? $table.'.' : '').$column; } public function find($id, $columns = ['*']) diff --git a/tests/Data/Entries/EntryQueryBuilderTest.php b/tests/Data/Entries/EntryQueryBuilderTest.php index b47c2f37..1116fa56 100644 --- a/tests/Data/Entries/EntryQueryBuilderTest.php +++ b/tests/Data/Entries/EntryQueryBuilderTest.php @@ -688,4 +688,42 @@ public function entries_are_found_using_offset() $this->assertCount(2, $entries); $this->assertEquals(['Post 2', 'Post 3'], $entries->map->title->all()); } + + /** @test */ + public function entries_can_be_retrieved_on_join_table_conditions() + { + Collection::make('posts')->save(); + EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'author' => 'John Doe', 'location' => 4])->create(); + EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'author' => 'John Doe'])->create(); + EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3', 'author' => 'John Doe', 'location' => 4])->create(); + Collection::make('locations')->save(); + + $locations = [ + 4 => ['slug' => 'shaldon', 'title' => 'Shaldon'], + 5 => ['slug' => 'cambridge', 'title' => 'Cambridge'], + 6 => ['slug' => 'london', 'title' => 'London'], + ]; + + foreach (range(4, 6) as $index) { + EntryFactory::id($index)->slug($locations[$index]['slug'])->collection('locations') + ->data(['title' => $locations[$index]['title']])->create(); + } + + $query = Entry::query() + ->join('entries as e', fn ($join) => $join + ->whereColumn('e.id', 'entries.id') + ->where('e.collection', 'posts') + )->leftJoin('entries as locations', function ($join) { + $join + ->where('locations.collection', 'locations') + ->on('locations.id', 'e.data->location'); + }) + ->where('e.data->title', 'like', '%post%') + ->where('locations.slug', 'shaldon'); + + $entries = $query->get(); + + // successfully retrieved 2 results + $this->assertCount(2, $entries); + } }