Skip to content

Commit

Permalink
Fix how bindings are merged into SQL query
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Feb 10, 2022
1 parent fd1757f commit d8d28bc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Casters/BuilderCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public function cast($target, Properties $properties, Stub $stub, bool $is_neste
protected function formatSql($sql, $bindings): string
{
$bindings = Arr::flatten($bindings);
$merged = preg_replace_callback('/\?/', fn() => DB::getPdo()->quote(array_shift($bindings)), $sql);
$merged = preg_replace_callback('/\?/', function() use (&$bindings) {
return DB::getPdo()->quote(array_shift($bindings));
}, $sql);

if (strlen($merged) > 120) {
$merged = SqlFormatter::format($merged, false);
Expand Down
24 changes: 24 additions & 0 deletions tests/DatabaseCasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ public function test_query_builder(): void
$this->assertDumpMatchesFormat($expected, $builder);
}

/** @see https://github.com/glhd/laravel-dumper/issues/6 */
public function test_where_between_statement(): void
{
$builder = User::where('name', 'test')
->whereBetween('id', [1, 2]);

$expected = <<<EOD
Illuminate\Database\Eloquent\Builder {
sql: "select * from "users" where "name" = 'test' and "id" between '1' and '2'"
#connection: Illuminate\Database\SQLiteConnection {
name: "testing"
database: ":memory:"
driver: "sqlite"
…%d
}
#model: Glhd\LaravelDumper\Tests\User { …}
#eagerLoad: []
…%d
}
EOD;

$this->assertDumpMatchesFormat($expected, $builder);
}

public function test_eloquent_builder(): void
{
$builder = User::query()
Expand Down

0 comments on commit d8d28bc

Please sign in to comment.