Skip to content

Commit

Permalink
Merge pull request #29602 from reinink/improve-from-subqueries-v2
Browse files Browse the repository at this point in the history
[6.0] Add subquery support for from() and table() - OPTION 2
  • Loading branch information
taylorotwell authored Aug 19, 2019
2 parents 83c997b + f75dad1 commit c451f22
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,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
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ public function testAddSelectWithSubQuery()
);
}

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 c451f22

Please sign in to comment.