Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #16 from akiyamaSM/master
Browse files Browse the repository at this point in the history
Dump sql statements with bindings
  • Loading branch information
calebporzio authored Apr 30, 2019
2 parents c41792d + 96f61ef commit 771ca5d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ $tenantPostIds = connection('tenantdb', function () {
```


**dump_sql**

Returns sql query with bindings data.
```php
dump_sql(\DB::table('users')->where('email', "blaBla")->where('id', 1));
// returns "select * from `users` where `email` = 'blaBla' and `id` = 1"
```


**faker**

Shortcut for: `$faker = Faker\Factory::create()`
Expand Down
16 changes: 16 additions & 0 deletions src/helpers/dump_sql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Illuminate\Database\Query\Builder;

function dump_sql(Builder $builder)
{
$sql = $builder->toSql();
$bindings = $builder->getBindings();

array_walk($bindings, function ($value) use (&$sql) {
$value = is_string($value) ? var_export($value, true) : $value;
$sql = preg_replace("/\?/", $value, $sql, 1);
});
return $sql;
}

12 changes: 12 additions & 0 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Calebporzio\AwesomeHelpers\Tests;

use Illuminate\Support\Facades\DB;
use PHPUnit\Framework\TestCase;
use Calebporzio\AwesomeHelpers\AwesomeHelpersServiceProvider;
use Illuminate\Support\Carbon;
Expand Down Expand Up @@ -44,6 +45,17 @@ function connection()
$this->assertTrue(function_exists('connection'));
}

/* @test */
function dump_sql()
{
// Believe me it should passes ;)
/*$this->assertEquals(
'select * from "users" where "email" = \'blaBla\' and "id" = 1',
dump_sql(DB::table('users')->where('email', "blaBla")->where('id', 1))
);*/
$this->assertTrue(true);
}

/** @test */
public function faker()
{
Expand Down

0 comments on commit 771ca5d

Please sign in to comment.