Skip to content

Commit 2a147a8

Browse files
committed
Allow setting connection by enum value in Model
1 parent 96a477d commit 2a147a8

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/Illuminate/Database/Eloquent/Factories/Factory.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Illuminate\Support\Traits\Macroable;
1818
use Throwable;
1919

20+
use function Illuminate\Support\enum_value;
21+
2022
/**
2123
* @template TModel of \Illuminate\Database\Eloquent\Model
2224
*
@@ -101,7 +103,7 @@ abstract class Factory
101103
/**
102104
* The name of the database connection that will be used to create the models.
103105
*
104-
* @var string|null
106+
* @var \UnitEnum|string|null
105107
*/
106108
protected $connection;
107109

@@ -156,7 +158,7 @@ abstract class Factory
156158
* @param \Illuminate\Support\Collection|null $for
157159
* @param \Illuminate\Support\Collection|null $afterMaking
158160
* @param \Illuminate\Support\Collection|null $afterCreating
159-
* @param string|null $connection
161+
* @param \UnitEnum|string|null $connection
160162
* @param \Illuminate\Support\Collection|null $recycle
161163
* @param bool|null $expandRelationships
162164
* @param array $excludeRelationships
@@ -802,16 +804,16 @@ public function withoutParents($parents = [])
802804
*/
803805
public function getConnectionName()
804806
{
805-
return $this->connection;
807+
return enum_value($this->connection);
806808
}
807809

808810
/**
809811
* Specify the database connection that should be used to generate models.
810812
*
811-
* @param string $connection
813+
* @param \UnitEnum|string $connection
812814
* @return static
813815
*/
814-
public function connection(string $connection)
816+
public function connection(\UnitEnum|string $connection)
815817
{
816818
return $this->newInstance(['connection' => $connection]);
817819
}

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
use ReflectionMethod;
3434
use Stringable;
3535

36+
use function Illuminate\Support\enum_value;
37+
3638
abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToString, HasBroadcastChannel, Jsonable, JsonSerializable, QueueableEntity, Stringable, UrlRoutable
3739
{
3840
use Concerns\HasAttributes,
@@ -52,7 +54,7 @@ abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToSt
5254
/**
5355
* The connection name for the model.
5456
*
55-
* @var string|null
57+
* @var \UnitEnum|string|null
5658
*/
5759
protected $connection;
5860

@@ -717,7 +719,7 @@ public function newInstance($attributes = [], $exists = false)
717719
* Create a new model instance that is existing.
718720
*
719721
* @param array<string, mixed> $attributes
720-
* @param string|null $connection
722+
* @param \UnitEnum|string|null $connection
721723
* @return static
722724
*/
723725
public function newFromBuilder($attributes = [], $connection = null)
@@ -726,7 +728,7 @@ public function newFromBuilder($attributes = [], $connection = null)
726728

727729
$model->setRawAttributes((array) $attributes, true);
728730

729-
$model->setConnection($connection ?: $this->getConnectionName());
731+
$model->setConnection($connection ?? $this->getConnectionName());
730732

731733
$model->fireModelEvent('retrieved', false);
732734

@@ -736,7 +738,7 @@ public function newFromBuilder($attributes = [], $connection = null)
736738
/**
737739
* Begin querying the model on a given connection.
738740
*
739-
* @param string|null $connection
741+
* @param \UnitEnum|string|null $connection
740742
* @return \Illuminate\Database\Eloquent\Builder<static>
741743
*/
742744
public static function on($connection = null)
@@ -1951,13 +1953,13 @@ public function getConnection()
19511953
*/
19521954
public function getConnectionName()
19531955
{
1954-
return $this->connection;
1956+
return enum_value($this->connection);
19551957
}
19561958

19571959
/**
19581960
* Set the connection associated with the model.
19591961
*
1960-
* @param string|null $name
1962+
* @param \UnitEnum|string|null $name
19611963
* @return $this
19621964
*/
19631965
public function setConnection($name)
@@ -1970,7 +1972,7 @@ public function setConnection($name)
19701972
/**
19711973
* Resolve a connection instance.
19721974
*
1973-
* @param string|null $connection
1975+
* @param \UnitEnum|string|null $connection
19741976
* @return \Illuminate\Database\Connection
19751977
*/
19761978
public static function resolveConnection($connection = null)

0 commit comments

Comments
 (0)