kingsleyuchenna's avatar

Store all phone numbers in one table

I am trying this concept I don't know if it is a wrong idea. So, I want to build an e-commerce site on my site I will have site contact (with phone numbers, emails, addresses etc), users shipping addresses, and I might have other places with phone numbers, emails, and addresses. So, my idea is to create a phone_numbers table to store all the numbers on the site, target columns will be model_name and model_id to target the owner (which is the user or site contact, or whichever model I am calling). I will do the same with emails and addresses.

0 likes
3 replies
LaryAI's avatar
Level 58

This is a good idea and it can be implemented in a few different ways. One way is to create a phone_numbers table with the following columns:

  • id: Primary key
  • model_name: The name of the model that owns the phone number
  • model_id: The ID of the model that owns the phone number
  • phone_number: The phone number

Then, you can create a relationship between the phone_numbers table and the other models that need to store phone numbers. For example, if you have a User model, you can create a one-to-many relationship between the User model and the phone_numbers table.

class User extends Model
{
    public function phoneNumbers()
    {
        return $this->hasMany(PhoneNumber::class);
    }
}

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

You can then use Eloquent to create, update, and delete phone numbers for a user.

$user = User::find(1);

// Create a new phone number
$user->phoneNumbers()->create([
    'phone_number' => '123-456-7890',
]);

// Update an existing phone number
$phoneNumber = $user->phoneNumbers()->first();
$phoneNumber->update([
    'phone_number' => '098-765-4321',
]);

// Delete an existing phone number
$phoneNumber = $user->phoneNumbers()->first();
$phoneNumber->delete();

This approach can be used for any model that needs to store phone numbers, emails, or addresses.

2 likes
webrobert's avatar

Yeah and if you add a couple columns you can label the numbers. Or even type like email, url, phone, etc so it become a contacts table

1 like
whoisthisstud's avatar
Level 7

I use both a single table for the record, and then a polymorphic table for the relation and any specific related columns, for both phone numbers and addresses in projects where they can be associated with multiple entities, otherwise a pivot table is all that you need.

The single table, say addresses would store the individual address record (address1, address2, city, state, zip, lat, long, etc..), and then a polymorphic table, addressables would relate that record with whatever other model you choose.

This way, multiple models (entities) can be associated with the same address but with varying related specific info, such as whether it is primary or secondary or active.

Email addresses tend to related to a single individual (User), so generally a pivot table is best in this regards IMO.

1 like

Please or to participate in this conversation.