Package provides additional, more efficient relationship methods for Laravel Eloquent models.
The laravel-extended-relationships package provides additional, more efficient relationship methods for Laravel Eloquent models. The package offers several useful features such as reducing the number of database queries, improving performance, and minimizing duplicate code.
I faced issue and made my own relationships then realise if I can use packages from open source then I can make one too and made this package.
You can install the package via composer:
composer require mr-punyapal/laravel-extended-relationships
First, include the HasExtendedRelationships
trait in your model:
use Mrpunyapal\LaravelExtendedRelationships\HasExtendedRelationships;
class Post extends Model {
use HasExtendedRelationships;
//...
}
Next, define the BelongsToManyKeys
relationship with the belongsToManyKeys
method:
public function auditors() {
return $this->belongsToManyKeys(
User::class,
'id',
[
'created_by' => 'creator',
'updated_by' => 'updater',
'deleted_by' => 'deleter'
]
);
}
User::class
)id
)['created_by' => 'creator', ...]
)
$post = Post::with('auditors')->first();
// Get the creator
$post->creator;
// Get the updater
$post->updater;
// Get the deleter
$post->deleter;
This allows you to define multiple relationships with just one method, and only a single query is fired in the database for all the relationships.
use Mrpunyapal\LaravelExtendedRelationships\HasExtendedRelationships;
class User extends Model{
use HasExtendedRelationships;
public function audited(){
return $this->hasManyKeys(
Post::class,
[
'created_by' => 'created',
'updated_by' => 'updated',
'deleted_by' => 'deleted'
],
'id'
);
}
}
To retrieve the audited posts of a user, you can use the audited relationship. Here's an example:
$user = User::with('audited')->first();
// Get posts created by the user
$user->created;
// Get posts updated by the user
$user->updated;
// Get posts deleted by the user
$user->deleted;
This allows you to define multiple relationships between models with a single method call, simplifying your code and reducing the number of queries executed.
If you have a column companies in your users table which stores an array of local keys like [7, 71], you can use the following relationship:
use Mrpunyapal\LaravelExtendedRelationships\HasExtendedRelationships;
class User extends Model
{
use HasExtendedRelationships;
protected $casts=[
'companies' => 'array'
];
public function myCompanies()
{
return $this->hasManyArrayColumn(
Company::class,
'id',
'companies'
);
}
}
When fetching data, you can retrieve the related companies with:
$user = User::with('myCompanies')->first();
// get companies with ids 7 and 71
$user->myCompanies;
This allows you to easily retrieve related records with an array of local keys, which can be useful in certain scenarios.
HasManyArrayColumn
The BelongsToArrayColumn
method allows you to define a relationship between a model and an array column on another model. Here's an example:
use Mrpunyapal\LaravelExtendedRelationships\HasExtendedRelationships;
class Company extends Model
{
use HasExtendedRelationships;
public function companyFounders()
{
return $this->belongsToArrayColumn(
User::class,
'id',
'companies'
);
}
}
With this relationship defined, you can fetch related company founders with the following code:
$company = Company::with('companyFounders')->find(71);
// Founders for company with id 71
$company->companyFounders;
This will provide you with data from the users
table where the companies
array column contains the value 71.
Right now, the BelongsToManyKeys
and HasManyKeys
methods work well with eager loading of the relation. However, when loading relations of a single model, the data may not be sorted as expected (e.g., in the order of "updater", "creator", etc.). Instead, all data will be returned as auditors. This functionality will be added in future updates.
While BelongsToArrayColumn
may work well with data such as [7,71]
, it may not function properly if the data in the database is ["7","71"]
. It is possible that this issue will be addressed in future updates.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.