Rafaelindriago's avatar

Best practices for manage address data in a Laravel app.

Hello guys, i'm developing a Laravel app and i need to save the address for multiple models, the client wants to use Google API for the app, i will really appreciate if you can give me some tips, some addresses contains coordinates and others until city, but the common format is country, state, city, postal code and optionally coordinates.

0 likes
5 replies
martinbean's avatar

@the raf I use schema.org’s naming conventions for addresses, where an address model has the following columns:

  • street_address
  • locality (town/city)
  • region (state, province, county, etc)
  • postal_code
  • country (a 2-character ISO code)

You can also store the coordinate, either in separate latitude and longitude columns, or in an actual geospatial column if you’re going to be querying them a lot.

2 likes
Rafaelindriago's avatar

@martinbean thank you!

You mean an model called "Address" with those columns? Then i make a relationship with the models that require to save the address.

martinbean's avatar

@The Raf Yeah. An Address model (and addresses table). You can then link to addresses using a foreign key.

If you’re creating addresses from Google Places look-ups, then you may want to also store the Google Place ID with addresses. That way, you can update or create rows using that ID, so you’re not inserting the same Google Place into your database multiple times.

1 like
Foks's avatar

@The Raf

It depends, in most cases I personally I find that having the fields on the parent model is easier to manage.

That would then mean that a user has a ``street_address, locality, region, postal_code, and contry` in the database, same goes for any other model.

However, if you really want to, you can make it a polymorphic relationship.

1 like
dhmm's avatar

I think you can use polymorphism here and depending on your requirements you can create a new address type. Maybe

class Address {} class GoogleAddress extends Address {}

3 likes

Please or to participate in this conversation.