I would not misjudge the way your database was modeled. In fact, its 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.
Does the project suggest 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 technology world it is often not very useful if you have an option to link more than one user to the same address . If your project is able to reach that point, keep this structure at any cost.
Does the project suggest that the same user has more than one address?
It is common for projects that require the possibility of multiple addresses. A very simple example is eCommerce. You have a) the billing address and b) the delivery address. For a second purchase, you may not want the same previous address, but you also do not want to remove it from the system for future purchases to be made easier. Excluding the previous possibility, this model would adopt a relation 1 - > N (1 user, N addresses) where the structure would be best if you removed the middle table and kept only the address table. In it, you would have a user_id that would solve your problem.
Does the project suggest that a user can never have more than one address?
In this case you can choose to a) keep the address data inside the user table or b) create a supplementary table.
When it comes to supplementary table, things can get a little complicated depending on how you decide to work. You can, for example, relate the id column of the user table directly with the id column of the address table. That way, if a user has registered an address, his address will be in the same id that he represents. If the user has not registered, the numbering of his id will be empty in the address table. Another common way would be to keep the same pattern as the previous question (1 -> N), but limit the code to only one address.
One way to achieve this is by specifying in the relation itself:
public function address(){
return $this->hasMany('App\Address')->first();
}
The first()
method in the relationship will ensure that you always load only one record. Just ensure that at the time of insertion, there is no record for the specified user.
A personal compliment that I suggest is that you never limit trivial things when there is no strict reason for doing so. It is much better that you return a list of addresses and ensure that Frontend traverses even one address than creating a limitation that in the future might require rework to undo the previously created limitation. Thus, if the frontend goes 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 to do so. Just rework the styling / appearance.