Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Andreas94's avatar

Create relationship hasMany with explode

Hi, I need to create a hasmany relationship, but unfortunately I have a problem.

The tables are composed as follows:

Giochi:
id - name - developer
1 - Call of Duty - 1
2 - Assassin's Creed - 2,3,4

Developer:

id - name
1- Activision
2 -Ubisoft Quebec
3 -Ubisoft Montreal
4 -Ubisoft

Since I put commas in developer ids, I had to do an "explode". So far I have always done so in the controllers:

$take_devs = explode(',', $giochi->developer);
$list_dev = array();
foreach($take_devs as $value)
{
    $list_dev[] = Developer::where('id', $value)->first();
}

Unfortunately, however, now, I should create a hasmany relationship to create a query

Developer::with('giochi')->get();

I tried with:

Models: Giochi
public function devs()
{
    $dev = Developer::where('slug', $this->slug)->first();
    $giochi = Giochi::all();

    $take_devs = explode(',', $this->developer);
    $list_dev = array();
    foreach($take_devs as $value)
    {
        $list_dev[] = Developer::where('id', $value)->first();
    }

}
Controller

$dev = Giochi::with('devs')->get();
    

But I get the error: Call to a member function addEagerConstraints() on null...

What is the most practical way to create a relationship between two tables when you have multiple column values?

0 likes
3 replies
mikevrind's avatar

I would suggest to migrate the data in the developer column to a pivot table. If you encounter something unworkable like this, make it work for the future.

But if you want to keep this database design error, here is a small suggestion. Don't use the foreach to select each developer in a separate query, use whereIn('id, $take_devs).

1 like
Andreas94's avatar

Hi @mikevrind, thanks for the replay.

As a pivot table, Is it structured well in this way?

INSERT INTO `developer_pivot` (`id`,`id_gioco`, `id_dev`)
VALUES ('1','1','4')
VALUES ('2','1','8')
VALUES ('3','2','51')
VALUES ('4','3','51')

Or in pivot tables the id is not necessary?

Edit:

I have created the relationship:

Models: Giochi.php
public function dev() {
return $this->belongsTo('App\Models\Developer_pivot', 'id', 'id_gioco');
    }

In database i have:

id - id_gioco -id_dev
2 - 1 - 4
3 - 1 - 51

Because if I put in the controller:

$test = Giochi::with('infogiochi','dev')->limit(15)->get();
return $test;

Does the JSon show me just a value?

dev: {
id: 3,
id_gioco: 1,
id_dev: 51
}
mikevrind's avatar
Level 7

Great you are fixing the real problem! The design for the pivot table is fine, you could always drop the 'id' column and make 'id_gioco' and 'id_dev' the PK of the table but I also know Eloquent loves to have that id column their, so you can leave the table this way.

With a pivot table you are building a many to many relationship. You don't need the belongsTo relation, you need belongsToMany. Please read the documentation about this type of relation: https://laravel.com/docs/5.4/eloquent-relationships#many-to-many.

In the belongsToMany relation you can state the related model (Developer), the name of the pivot table (developer_pivot), the foreign key and related key. Eloquent will then load the developers via your pivot table. Hopefully you can resolve your issue via the information in the documentation. Just let us know if you cant :)

1 like

Please or to participate in this conversation.