Skip to content

Commit b28bede

Browse files
committed
Fixed to use the correct connection at runtime as setting config values in middleware can happen after a controller is resolved
1 parent c428388 commit b28bede

2 files changed

Lines changed: 8 additions & 18 deletions

File tree

src/Models/Model.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,11 @@ class Model extends EloquentModel
3131
public const PUBLIC_ID_COLUMN = 'public_id';
3232

3333
/**
34-
* Create a new instance of the model.
35-
*
36-
* @param array $attributes the attributes to set on the model
37-
*
38-
* @return void
34+
* Get the correct current connection to use.
3935
*/
40-
public function __construct(array $attributes = [])
36+
public function getConnectionName()
4137
{
42-
parent::__construct($attributes);
43-
44-
$this->connection = config('fleetbase.connection.db', 'mysql');
38+
return $this->connection ?: config('fleetbase.connection.db', 'mysql');
4539
}
4640

4741
/**

src/Models/Setting.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,13 @@ class Setting extends EloquentModel
2121
use Filterable;
2222

2323
/**
24-
* Create a new instance of the model.
25-
*
26-
* @param array $attributes the attributes to set on the model
27-
*
28-
* @return void
24+
* Get the correct current connection to use.
25+
* NOTE FOR LATER: Perhaps system level settings should always write to main connection,
26+
* but allow organization level settings to write to sandbox connection.
2927
*/
30-
public function __construct(array $attributes = [])
28+
public function getConnectionName()
3129
{
32-
parent::__construct($attributes);
33-
34-
$this->connection = config('fleetbase.connection.db', 'mysql');
30+
return $this->connection ?: config('fleetbase.connection.db', 'mysql');
3531
}
3632

3733
/**

0 commit comments

Comments
 (0)