diff --git a/README.md b/README.md index b30a3404e..5b8e14442 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ This package adds functionalities to the Eloquent model and Query builder for Mo - [Cross-Database Relationships](#cross-database-relationships) - [Authentication](#authentication) - [Queues](#queues) + - [Debugging](#debugging) + - [->toSql()](#tosql) - [Upgrading](#upgrading) - [Upgrading from version 2 to 3](#upgrading-from-version-2-to-3) @@ -731,7 +733,7 @@ The belongsToMany relation will not use a pivot "table" but will push id's to a If you want to define custom keys for your relation, set it to `null`: ```php -use Jenssegers\Mongodb\Eloquent\Mode; +use Jenssegers\Mongodb\Eloquent\Model; class User extends Model { @@ -1075,6 +1077,24 @@ Last, add the service provider in `config/app.php`: Jenssegers\Mongodb\MongodbQueueServiceProvider::class, ``` +Debugging +--------- + +### ->toSql() + +Hence we are in a NoSQL driver, it is counter-intuitive to use `toSql()` to output the string that will be executed. + +However, if you still need the string that looks like the sql for some inspection, you can use the `toSql()` method: + +```php +User::where('some_field', '>', 300) + ->whereRaw(['age' => ['$gt' => 30, '$lt' => 40]]) + ->toSql(); + +// output +"select * from "users" where "some_field" > ? and {"age":{"$gt":30,"$lt":40}}" +``` + Upgrading --------- diff --git a/src/Jenssegers/Mongodb/Query/Grammar.php b/src/Jenssegers/Mongodb/Query/Grammar.php index 3b1d33a8a..1905301e8 100644 --- a/src/Jenssegers/Mongodb/Query/Grammar.php +++ b/src/Jenssegers/Mongodb/Query/Grammar.php @@ -6,4 +6,19 @@ class Grammar extends BaseGrammar { + /** + * {@inheritdoc} + */ + protected function compileWheresToArray($query) + { + return collect($query->wheres)->map(function ($where) use ($query) { + $baseWhere = $this->{"where{$where['type']}"}($query, $where); + + if (isset($where['sql']) && is_array($where['sql'])) { + $baseWhere = json_encode($baseWhere); + } + + return $where['boolean'].' '.$baseWhere; + })->all(); + } } diff --git a/tests/ModelTest.php b/tests/ModelTest.php index ab9ad63b8..e460d174a 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -440,7 +440,7 @@ public function testCarbonDateMockingWorks() Carbon::setTestNow($fakeDate); $item = Item::create(['name' => 'sword']); - + $this->assertLessThan(1, $fakeDate->diffInSeconds($item->created_at)); } @@ -572,4 +572,25 @@ public function testChunkById(): void $this->assertEquals(3, $count); } + + public function testToSql(): void + { + $this->assertTrue( + is_string(User::where('age', 30)->toSql()) + ); + + $this->assertTrue( + is_string(User::whereRaw(['age' => ['$gt' => 30, '$lt' => 40]])->toSql()) + ); + + $this->assertEquals( + 'select * from "users" where "some_field" > ?', + User::where('some_field', '>', 300)->toSql() + ); + + $this->assertEquals( + 'select * from "users" where "some_field" > ? and {"age":{"$gt":30,"$lt":40}}', + User::where('some_field', '>', 300)->whereRaw(['age' => ['$gt' => 30, '$lt' => 40]])->toSql() + ); + } }