Skip to content

Commit

Permalink
Fix support for pivot models
Browse files Browse the repository at this point in the history
  • Loading branch information
bennothommo committed Jul 29, 2024
1 parent 3fcb603 commit c4abef8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -849,12 +849,10 @@ public function newPivot(EloquentModel $parent, array $attributes, $table, $exis
*/
public function newRelationPivot($relationName, $parent, $attributes, $table, $exists)
{
$definition = $this->getRelationDefinition($relationName);
$relation = $this->{$relationName}();
$pivotModel = $relation->getPivotClass();

if (!is_null($definition) && array_key_exists('pivotModel', $definition)) {
$pivotModel = $definition['pivotModel'];
return $pivotModel::fromAttributes($parent, $attributes, $table, $exists);
}
return $pivotModel::fromAttributes($parent, $attributes, $table, $exists);
}

//
Expand Down
9 changes: 9 additions & 0 deletions src/Database/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany as BelongsToManyBase;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Winter\Storm\Database\Pivot;

class BelongsToMany extends BelongsToManyBase implements Relation
{
Expand Down Expand Up @@ -35,6 +36,14 @@ public function __construct(
$this->extendableRelationConstruct();
}

/**
* {@inheritDoc}
*/
public function getPivotClass()
{
return $this->using ?? Pivot::class;

Check failure on line 44 in src/Database/Relations/BelongsToMany.php

View workflow job for this annotation

GitHub Actions / Code Analysis

Property Illuminate\Database\Eloquent\Relations\BelongsToMany<Illuminate\Database\Eloquent\Model>::$using (string) on left side of ?? is not nullable.
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit c4abef8

Please sign in to comment.