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:
Require package with composer
composer require payter/has-column-many
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) {
//
}
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']);
You don't need to do anything, it will automatically asume if it's comma separated or JSON or Serialized.