Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if AsPivot trait is used instead of Pivot Model #35271

Merged
merged 1 commit into from Nov 18, 2020
Merged

check if AsPivot trait is used instead of Pivot Model #35271

merged 1 commit into from Nov 18, 2020

Conversation

Gummibeer
Copy link
Contributor

Right now the logic that allows using a class name of a pivot model as $table argument and resolves its table and also uses it as pivot model only checks for the default \Illuminate\Database\Eloquent\Relations\Pivot model class.

The problem with this is that the real thing that makes a pivot model is the \Illuminate\Database\Eloquent\Relations\Concerns\AsPivot trait.

This PR changes this condition to check for the trait instead of an instance check for the base class.

The benefit is that the user is able to create his own pivot base model that doesn't have to be based on the default pivot model.

As an example - that's our custom pivot model in one app that's based on our custom base model.

namespace App\Eloquent;

use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot;
use Illuminate\Support\Str;

abstract class Pivot extends Model
{
    use AsPivot;

    public $incrementing = true;

    public function getTable(): string
    {
        return $this->table ?? Str::snake(Str::singular(Str::replaceLast('Pivot', '', class_basename($this))));
    }
}

@taylorotwell taylorotwell merged commit abea927 into laravel:8.x Nov 18, 2020
@Gummibeer Gummibeer deleted the patch-2 branch November 18, 2020 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants