Skip to content

Latest commit

 

History

History
66 lines (44 loc) · 1.59 KB

README.md

File metadata and controls

66 lines (44 loc) · 1.59 KB

Comma separated IDs, JSON and PHP Serialized relationships for Laravel Eloquent models

You will be able to make relations of comma separated values in your MySQL table super easely, with adding just few lines of code into your Eloquent model.

It

So if you use Laravel and your relations in the table looks like this, then this package is for you:

Comma separated example PHP Serialized example

Installation

Require package with composer

composer require payter/has-column-many

Basic usage

Using of this package is super easy - you just need to include UseColumnMany trait to your model.

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use PayteR\UseColumnMany;

class ModelName extends Model
{
    use UseColumnMany;

    public function relateds()
    {
        return $this->hasColumnMany(RelatedModel::class, 'related_ids');
    }
}

And you can call it like any other Eloquent relationships

use App\Models\ModelName;
 
$relateds = ModelName::find(1)->relateds;
 
foreach ($relateds as $related) {
    //
}

Advanced usage

Multiple columns

You are able to concat ID's even from more than one column! It's easy - just pass an array of column names as second parameter:

return $this->hasColumnMany(RelatedModel::class, ['related_ids', 'another_column_ids']);

JSON and Serialised ID's

You don't need to do anything, it will automatically asume if it's comma separated or JSON or Serialized.

You can