inani / lazy-belongs-to-many
A lightweight implementation of Laravel's BelongsToMany relationship
Installs: 31
Dependents: 0
Suggesters: 0
Security: 0
Stars: 24
Watchers: 3
Forks: 2
Open Issues: 0
pkg:composer/inani/lazy-belongs-to-many
Requires
- illuminate/database: ^8.0
- illuminate/support: ~8
Requires (Dev)
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2025-09-18 01:24:38 UTC
README
A lightweight implementation of Laravel's belongs To many relationship in cases you don't need pivot table.
Installation
First, install the package through Composer.
composer require inani/lazy-belongs-to-many
Usage
Suppose we have the following database structure. A User
can be linked to one or many Tag
. instead of creating the pivot table we can save tags id list in a column on the user table.
| id | name | tags_id |
|----|-------------------|---------------|
| 1 | El Houssain inani | [1, 10, 4, 33]|
<?php namespace Models; use Illuminate\Database\Eloquent\Model; use Inani\LazyBelongsToMany\Relations\BelongsToManyCreator; class User extends Model { use BelongsToManyCreator; protected $casts = [ 'tags_id' => 'array' // <=== IMPORTANT ]; /** * Tags * @return mixed */ public function tags() { return $this->belongsToManyInArray(Tag::class); } }
For the time being there is no implementation for the inverse. I'll be happy to see your contribution at any way.
Happy Coding.