diff --git a/src/Query/Builder.php b/src/Query/Builder.php index ada341d82f..b41e38b17c 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -129,6 +129,11 @@ public function whereNotIn($column, $values) return $this; } + public function find($id, $columns = ['*']) + { + return $this->where('id', $id)->get($columns)->first(); + } + public function first() { return $this->get()->first(); diff --git a/tests/Query/BuilderTest.php b/tests/Query/BuilderTest.php new file mode 100644 index 0000000000..11da821975 --- /dev/null +++ b/tests/Query/BuilderTest.php @@ -0,0 +1,43 @@ +save(); + + EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'author' => 'John Doe'])->create(); + $entry = 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'])->create(); + + return $entry; + } + + /** @test **/ + public function entry_is_found_within_all_created_entries_using_entry_facade_with_find_method() + { + $searchedEntry = $this->createDummyCollectionAndEntries(); + $retrievedEntry = Entry::query()->find($searchedEntry->id()); + + $this->assertSame($searchedEntry, $retrievedEntry); + } + + /** @test **/ + public function entry_is_found_within_all_created_entries_and_select_query_columns_are_set_using_entry_facade_with_find_method_with_columns_param() + { + $searchedEntry = $this->createDummyCollectionAndEntries(); + $columns = ['title']; + $retrievedEntry = Entry::query()->find($searchedEntry->id(), $columns); + + $this->assertSame($searchedEntry, $retrievedEntry); + $this->assertSame($retrievedEntry->selectedQueryColumns(), $columns); + } +}