troelskn / laravel-fillable-relations

Provides HasFillableRelations trait to Eloquent models
63 stars 23 forks source link

getForeignKey does not exist #9

Closed tostercx closed 6 years ago

tostercx commented 6 years ago

https://github.com/troelskn/laravel-fillable-relations/blob/1ef111fb7292098feb79108b77a8e630678a7760/src/Eloquent/Concerns/HasFillableRelations.php#L127

Seems Laravel renamed this method to getQualifiedForeignKeyName in 5.4?

https://github.com/laravel/framework/blob/5.3/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php#L378

https://github.com/laravel/framework/blob/5.4/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php#L413

There's also a getForeignKeyName now, not sure what's the difference between the two.

edit

I guess getForeignKeyName takes the part after the last dot, so there's no need to do str_after now.

edit

Just noticed there's already a similar fix for HasMany relations, this bug is just for HasOne.

https://github.com/troelskn/laravel-fillable-relations/blob/1ef111fb7292098feb79108b77a8e630678a7760/src/Eloquent/Concerns/HasFillableRelations.php#L148-L153

tostercx commented 6 years ago

Unrelated to this issue but in the same function - I think HasOne relations usually work in the opposite direction?

https://github.com/troelskn/laravel-fillable-relations/blob/1ef111fb7292098feb79108b77a8e630678a7760/src/Eloquent/Concerns/HasFillableRelations.php#L130

As in the foreign table has a key that links back to this table.

https://laravel.com/docs/5.6/eloquent-relationships#one-to-one

tostercx commented 6 years ago

Got it running for me with some dirty hacks :)

public function fillHasOneRelation(HasOne $relation, $attributes, $relationName)
{
    $this->fillHasManyRelation($relation, [$attributes], $relationName);
}

// removed HasMany type
public function fillHasManyRelation($relation, array $attributes, $relationName)
{
    ...
}

You might consider merging HasOne / HasMany in some (more elegant) way. It's basically the same kind of relation.

edit

Less hacky - type checking on 2 public methods, then proxying to a private non-typed.

https://github.com/tostercx/laravel-fillable-relations/commit/0a02781e920dececda37ace50179e1600b98fe89

troelskn commented 6 years ago

Hey @tostercx thanks for your detective work. I hope to have time to look at this in a not too far future, but not sure when.