MMIT's avatar
Level 5

Understanding One to Many relationships with a pivot table?

Hello All,

I have two main models:

user phone

The user hasMany phone numbers, and a phone number belongsTo a user.

I have a table for users and a table for phone numbers, but the phone number model also belongsTo other models in my application, such as a teams model, a customer model, etc.

I've created a table called user_phone and it's essentially a pivot table with:

Schema::create('users_phone', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable(false)->constrained('users',
                indexName: 'user_id')->onDelete('cascade');
            $table->foreignId('phone_id')->nullable(false)->constrained('phones',
                indexName: 'phone_id')->onDelete('cascade');
            $table->timestamps();
        });

But I can't get the relationship to work in my seeder. in my user class i have:

public function phones(): HasMany
    {
        return $this->hasMany(Phone::class);
    }

and in my phone class I have:

  public function users(): belongsTo
    {
        return $this->belongsTo(User::class);
    }

here is my seeder:

        $users = User::factory(9)->has(phone::factory()->count(2))->create();

Any advice on how to handle when you have a model that will have many relationships with other models in a oneToMany relationship?

Thanks so much!

0 likes
2 replies
LaryAI's avatar
Level 58

In your scenario, it seems like you're trying to use a pivot table for a one-to-many relationship, which is not necessary. Pivot tables are typically used for many-to-many relationships. Since a user can have many phones, and a phone belongs to a user, you should not need a pivot table for this relationship.

Here's how you can set up your models and database schema correctly for a one-to-many relationship:

Database Schema

You should have a phones table with a user_id foreign key to establish the relationship:

Schema::create('phones', function (Blueprint $table) {
    $table->id();
    $table->string('number');
    $table->foreignId('user_id')->constrained()->onDelete('cascade');
    $table->timestamps();
});

User Model

In your User model, you should define the hasMany relationship:

public function phones(): HasMany
{
    return $this->hasMany(Phone::class);
}

Phone Model

In your Phone model, you should define the belongsTo relationship:

public function user(): BelongsTo
{
    return $this->belongsTo(User::class);
}

Seeder

In your seeder, you can use the has method to create users with phones:

$users = User::factory(9)->has(Phone::factory()->count(2))->create();

Explanation

  • User Model: The hasMany relationship indicates that a user can have multiple phone numbers.
  • Phone Model: The belongsTo relationship indicates that each phone number belongs to a single user.
  • Seeder: The has method is used to create users with a specified number of related phone records.

By setting up your models and database schema this way, you can effectively manage a one-to-many relationship without needing a pivot table. If you have other models that also need to relate to phones, you can add similar belongsTo relationships in those models.

MMIT's avatar
Level 5

Hello All -

I need a polymorphic relationship.. Thanks!

Please or to participate in this conversation.