Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.

Commit 56469ce

Browse files
committed
add possibility to set an alias
1 parent e293a6f commit 56469ce

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/Repositories/PostgresqlFunctionRepository.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,38 @@ public function __construct(
3131
*
3232
* @param string $name
3333
* @param array $parameters
34+
* @param string $as
3435
*
3536
* @return array
3637
*/
37-
public function runDatabaseFunction(string $name, array $parameters = []): array
38+
public function runDatabaseFunction(string $name, array $parameters = [], string $as = ''): array
3839
{
3940
return DB::connection($this->connection)->select(
40-
$this->buildSqlFunction($name, $parameters),
41+
$this->buildSqlFunction($name, $parameters, $as),
4142
$parameters
4243
);
4344
}
4445

4546
/**
4647
* @param string $name
4748
* @param array $parameters
49+
* @param string $as
4850
*
4951
* @return string
5052
*/
51-
protected function buildSqlFunction(string $name, array $parameters): string
53+
protected function buildSqlFunction(string $name, array $parameters, string $as): string
5254
{
5355
$prepareForBindings = collect($parameters)->implode(fn () => '?, ');
5456

55-
return $this->select . $name . str($prepareForBindings)
56-
->replaceLast(', ', '')
57-
->start('(')
58-
->finish(')');
57+
$function = str($prepareForBindings)
58+
->replaceLast(', ', '')
59+
->start('(')
60+
->finish(')');
61+
62+
if (! empty($as)) {
63+
$function = $function->finish(' as ' . $as);
64+
}
65+
66+
return $this->select . $name . $function;
5967
}
6068
}

0 commit comments

Comments
 (0)