tjventurini / get-relationship-key
Trait and sample configuration to manage model relationships via configurations.
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 224
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
Requires
- illuminate/support: ^6.0
This package is auto-updated.
Last update: 2020-10-08 16:56:29 UTC
README
This package includes a trait and sample configuration to manage your model relationships via the configurations. The idea is that you don't have add methods on models for each relationship, but rather create traits that add these methods to the model. That way you only have to create a trait for each model and not for each relationship.
Installation
To install this package you just have to require it through composer.
composer require tjventurini/get-relationship-key
Usage
Include the trait on your models.
use Tjventurini\GetRelationshipKey\GetRelationshipKey; ... class User extends Authenticatable { ... use GetRelationshipKey; }
Then you can call $this->getRelationshipKey()
to get a camelcase representation of your models class name.Take the
following examples.
User => user
OrderItem => order_item
SimpleXML => simple_xml
Now instead of adding relationships directly on your model you create traits that will handle this for you. Check out
the Traits Tjventurini\GetRelationshipKey\BelongsToUser
and Tjventurini\GetRelationshipKey\BelongsToManyUsers
to
find out how you can write such traits on your own.
Configuration
To configure the BelongsToUser
and BelongsToManyUsers
table, that come with this package, just add the following
to your config/database.php
and add your values accordingly.
/* |-------------------------------------------------------------------------- | Relationships |-------------------------------------------------------------------------- | | In this section you will find all eloquent relationships and the values | to configure them. | */ 'tables' => [ 'user' => 'users', // pivot tables // eg.: // 'user_page' => 'user_page', // 'page_user' => 'page_user', ], 'models' => [ 'user' => \App\User::class, ], 'foreign_keys' => [ 'user' => 'user_id', ],
Now you should be able to just add your relationship traits on the models where you need them instead of adding the same methods on multiple models.