Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Entries/EntryQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand All @@ -43,7 +46,7 @@ protected function column($column)
}
}

return $column;
return ($table ? $table.'.' : '').$column;
}

public function find($id, $columns = ['*'])
Expand Down
38 changes: 38 additions & 0 deletions tests/Data/Entries/EntryQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}