Skip to content

Add ability to customize pivot columns #175

@kennyhorna

Description

@kennyhorna

Hi, amazing work by the way!

Current situation

Reviewing the docs I can see that you can specify belongsToMany relationships like so:

# draft.yaml

models:
  User:
    ...
    relationships:
      belongsToMany: Store

This will generate something similar to the following method in the User model:

# User.php

public function stores()
{
    return $this->belongsToMany(Store::class);
}

But, currently, there's no way to customize the pivot table name: store_user

Proposal

It could be possible to pass an attribute when specifying the relationship to customize the pivot table:

models:
  User:
    ...
    relationships:
      belongsToMany: Store:favorites

Generating:

# User.php

public function stores()
{
    return $this->belongsToMany(Store::class, 'favorites');
}

Even further, in case you specify the pivot colum as a PascalCase word, this could generate the pivot model and configure it in the related models:

#draft.yaml

models:
  User:
    ...
    relationships:
      belongsToMany: Store:Favorite
# User.php

public function stores()
{
    return $this->belongsToMany(Store::class)->using(Favorite::class);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions