Skip to content

Commit e2a8fae

Browse files
goodevilgeniusjenssegers
authored andcommitted
Return proper value instead of _id on QueryBuilder (#1747)
Fixes #1741
1 parent cc66b3a commit e2a8fae

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Jenssegers/Mongodb/Query/Builder.php

+10
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,16 @@ public function find($id, $columns = [])
204204
return $this->where('_id', '=', $this->convertKey($id))->first($columns);
205205
}
206206

207+
/**
208+
* @inheritdoc
209+
*/
210+
public function value($column)
211+
{
212+
$result = (array) $this->first([$column]);
213+
214+
return Arr::get($result, $column);
215+
}
216+
207217
/**
208218
* @inheritdoc
209219
*/

tests/QueryBuilderTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -701,4 +701,16 @@ public function testProjections()
701701
$this->assertEquals(1, count($result['tags']));
702702
}
703703
}
704+
705+
public function testValue()
706+
{
707+
DB::collection('books')->insert([
708+
['title' => 'Moby-Dick', 'author' => ['first_name' => 'Herman', 'last_name' => 'Melville']]
709+
]);
710+
711+
$this->assertEquals('Moby-Dick', DB::collection('books')->value('title'));
712+
$this->assertEquals(['first_name' => 'Herman', 'last_name' => 'Melville'], DB::collection('books')->value('author'));
713+
$this->assertEquals('Herman', DB::collection('books')->value('author.first_name'));
714+
$this->assertEquals('Melville', DB::collection('books')->value('author.last_name'));
715+
}
704716
}

0 commit comments

Comments
 (0)