inani/lazy-belongs-to-many

A lightweight implementation of Laravel's BelongsToMany relationship

dev-main 2021-11-01 17:17 UTC

This package is auto-updated.

Last update: 2024-12-17 23:28:52 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.