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.