Skip to content

Commit c750c21

Browse files
Fix sorting by numbers when using collection in condition (#513)
* Fix sorting by floats when using collection `in` condition * why doesnt pint catch this --------- Co-authored-by: Ryan Mitchell <[email protected]>
1 parent ac57833 commit c750c21

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/Entries/EntryQueryBuilder.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,15 @@ protected function getJsonCasts(): IlluminateCollection
214214
$wheres = collect($this->builder->getQuery()->wheres);
215215
$collectionWhere = $wheres->firstWhere('column', 'collection');
216216

217-
if (
218-
! $collectionWhere
219-
|| ! isset($collectionWhere['value'])
220-
|| ! ($collection = Collection::find($collectionWhere['value']))
221-
) {
217+
if (! $collectionWhere) {
218+
return collect([]);
219+
}
220+
221+
if (isset($collectionWhere['values']) && count($collectionWhere['values']) == 1) {
222+
$collectionWhere['value'] = $collectionWhere['values'][0];
223+
}
224+
225+
if (! isset($collectionWhere['value']) || ! $collection = Collection::find($collectionWhere['value'])) {
222226
return collect([]);
223227
}
224228

tests/Data/Entries/EntryQueryBuilderTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -770,14 +770,19 @@ public function entries_can_be_ordered_by_a_float_json_field()
770770
Blueprint::shouldReceive('in')->with('collections/posts')->andReturn(collect(['posts' => $blueprint]));
771771

772772
Collection::make('posts')->save();
773-
EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'float' => 3.3])->create();
774-
EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'float' => 5.5])->create();
775-
EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3', 'float' => 1.1])->create();
773+
EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'float' => '9.5'])->create();
774+
EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'float' => '10.2'])->create();
775+
EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3', 'float' => '8.7'])->create();
776776

777777
$entries = Entry::query()->where('collection', 'posts')->orderBy('float', 'asc')->get();
778778

779779
$this->assertCount(3, $entries);
780780
$this->assertEquals(['Post 3', 'Post 1', 'Post 2'], $entries->map->title->all());
781+
782+
$entries = Entry::query()->whereIn('collection', ['posts'])->orderBy('float', 'asc')->get();
783+
784+
$this->assertCount(3, $entries);
785+
$this->assertEquals(['Post 3', 'Post 1', 'Post 2'], $entries->map->title->all());
781786
}
782787

783788
#[Test]

0 commit comments

Comments
 (0)