Skip to content

Commit

Permalink
Add subquery support for "from" and "table"
Browse files Browse the repository at this point in the history
  • Loading branch information
reinink committed Aug 17, 2019
1 parent df60917 commit 6c1e014
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/Illuminate/Database/Capsule/Manager.php
Expand Up @@ -77,13 +77,14 @@ public static function connection($connection = null)
/**
* Get a fluent query builder instance.
*
* @param string $table
* @param \Closure|\Illuminate\Database\Query\Builder|string $table
* @param string|null $as
* @param string|null $connection
* @return \Illuminate\Database\Query\Builder
*/
public static function table($table, $connection = null)
public static function table($table, $as = null, $connection = null)
{
return static::$instance->connection($connection)->table($table);
return static::$instance->connection($connection)->table($table, $as);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Database/Connection.php
Expand Up @@ -257,12 +257,13 @@ public function getSchemaBuilder()
/**
* Begin a fluent query against a database table.
*
* @param string $table
* @param \Closure|\Illuminate\Database\Query\Builder|string $table
* @param string|null $as
* @return \Illuminate\Database\Query\Builder
*/
public function table($table)
public function table($table, $as = null)
{
return $this->query()->from($table);
return $this->query()->from($table, $as);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Database/ConnectionInterface.php
Expand Up @@ -9,10 +9,11 @@ interface ConnectionInterface
/**
* Begin a fluent query against a database table.
*
* @param string $table
* @param \Closure|\Illuminate\Database\Query\Builder|string $table
* @param string|null $as
* @return \Illuminate\Database\Query\Builder
*/
public function table($table);
public function table($table, $as = null);

/**
* Get a new raw query expression.
Expand Down
13 changes: 10 additions & 3 deletions src/Illuminate/Database/Query/Builder.php
Expand Up @@ -367,12 +367,19 @@ public function distinct()
/**
* Set the table which the query is targeting.
*
* @param string $table
* @param \Closure|\Illuminate\Database\Query\Builder|string $table
* @param string|null $as
* @return $this
*/
public function from($table)
public function from($table, $as = null)
{
$this->from = $table;
if ($table instanceof self ||
$table instanceof EloquentBuilder ||
$table instanceof Closure) {
return $this->fromSub($table, $as);
}

$this->from = $as ? "{$table} as {$as}" : $table;

return $this;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Integration/Database/QueryBuilderTest.php
Expand Up @@ -31,6 +31,21 @@ protected function setUp(): void
]);
}

public function testFromWithAlias()
{
$this->assertSame('select * from "posts" as "alias"', DB::table('posts', 'alias')->toSql());
}

public function testFromWithSubQuery()
{
$this->assertSame(
'Fake Post',
DB::table(function ($query) {
$query->selectRaw("'Fake Post' as title");
}, 'posts')->first()->title
);
}

public function testWhereDate()
{
$this->assertSame(1, DB::table('posts')->whereDate('created_at', '2018-01-02')->count());
Expand Down

0 comments on commit 6c1e014

Please sign in to comment.