I wouldn’t misjudge the way your database was modeled. In fact, your structure is already prepared to accept more than one address per user and/or link an existing address to a different user. However, you would need to think better about the business rather than just the code / programming.
The project suggests the possibility of registering more than one user at the same address?
Although this is a reality (people usually share the same address with parents, spouses, republics, etc.), in the world of technology it is not usually very useful to have an option to link more than one user to the same address. If your project is able to reach this point, keep this structure at any cost.
The project suggests that the same user has more than one address?
It is common projects that require the possibility of multiple addresses. A very simple example is eCommerce. You have to) the billing address and b) the delivery address. For a second purchase, you may not want the same previous address, but you also don’t want to remove it from the system to make future purchases easier. Excluding the previous possibility, this model would adopt a relationship 1 -> N (1 user, N addresses) where the structure would be better if you removed the middle table and kept only the table address. In it, you’d have one user_id that would solve your problem.
The project suggests that a user can never have more than one address?
In this case you can choose to a) keep the address data within the user table or b) create a supplementary table.
When the subject is supplementary table, things can get a little complicated depending on how you decide to work. You can, for example, relate the column id table user directly with the column id table address. This way, if a user has registered an address, its address will be in the same id that the user represents. If the user has not registered, the numbering of his id will be empty in the table address. Another common way would be to maintain the same pattern as the previous question (1 -> N), but to limit in code the registration of only one address.
One way to achieve this is by specifying in the relationship itself:
public function address(){
return $this->hasMany('App\Address')->first();
}
The method first()
in the relationship will ensure that you will always carry only one record. Simply ensure that at the time of insertion, there is no registration for the specified user.
A personal addition I suggest is that you never limit trivial things when there is no strict reason for it. It is much better if you return a list of addresses and ensure that Frontend travels even if only by one address than creating a limitation that in the future may require reworking to undo the previously created limitation. Thus, if the frontend travels through the array with only 1 record and in the future the project decides to accept more than 1 address per user, its code is prepared for this. Just rework the styling/appearance.
In fact the User entity is like a basic registration for the application, there are other relationships to indicate if the customer is Admin, Super, Seller and the like. For some there are addresses and for others there are not. So for User the project suggests that it will always be just ONE address.
– touchmx
@touchmx in this case I suggest case 1 -> N instead of your N <-> N modeling. You can then limit as I suggested using the
->first()
or follow my second suggestion to never limit and always enable a future change more easily– Marco Aurélio Deleu