You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Having a model, without $connection property specified, as a relation from another model with explicit $connection causes the child to use the parent's connection instead of the default connection.
I found out this behavior was implemented in #16103 but I think it might not be the desired behavior.
According with what the documentation says, it should use the default connection (and that was exactly what I was expecting):
By default, all Eloquent models will use the default database connection that is configured for your application. If you would like to specify a different connection that should be used when interacting with a particular model, you should define a $connection property on the model:
Proposed solution:
I think it could be implemented something like this:
Then in the model you want to inherit the parent's connection:
<?phpnamespaceApp\Models;
useIlluminate\Database\Eloquent\Model;
class Child extends Model
{
protected$modelShouldInheritConnection = true;
}
Or it could be set to true by default so as not to break the current behavior.
Kind regards!
Steps To Reproduce
Child Model:
<?phpnamespaceApp\Models;
useIlluminate\Database\Eloquent\Model;
class Child extends Model
{
//
}
Parent Model:
<?phpnamespaceApp\Models;
useIlluminate\Database\Eloquent\Model;
useIlluminate\Database\Eloquent\Relations\BelongsTo;
class Parent extends Model
{
protected$connection = 'another';
publicfunctionchild(): BelongsTo
{
// `Child` model will use `another` connection instead of the defaultreturn$this->belongsTo(Child::class);
}
}
The text was updated successfully, but these errors were encountered:
Laravel Version
11.19.0
PHP Version
8.3.8
Database Driver & Version
MySQL 5.7.44
Description
Having a model, without
$connection
property specified, as a relation from another model with explicit$connection
causes the child to use the parent's connection instead of the default connection.I found out this behavior was implemented in #16103 but I think it might not be the desired behavior.
According with what the documentation says, it should use the default connection (and that was exactly what I was expecting):
Proposed solution:
I think it could be implemented something like this:
Model.php
HasRelationships.php
Then in the model you want to inherit the parent's connection:
Or it could be set to
true
by default so as not to break the current behavior.Kind regards!
Steps To Reproduce
Child Model:
Parent Model:
The text was updated successfully, but these errors were encountered: