8

I need to save Ip's in the database. I am using laravel but I need to store IPv6 and IPv4 ip's Is the ip type ready for IP?

$table->ipAddress('visitor');

Or do I need to use a the normal string type.

Thanks

patricus
  • 59,488
  • 15
  • 143
  • 145
KBeckers
  • 416
  • 4
  • 12
  • In Postgres it creates an `INET` column type, which [the docs](https://www.postgresql.org/docs/9.1/static/datatype-net-types.html) say is suitable for both. I can't speak to MySQL. You can test this yourself in about 30 seconds (as I did). – ceejayoz Jul 08 '16 at 17:54
  • I was just curious. Gonna test it now. – KBeckers Jul 08 '16 at 17:57

1 Answers1

12

The ipAddress() method creates the following field types for the specified databases:

  • MySql - varchar(45)
  • SqlServer - nvarchar(45)
  • Postgres - inet
  • SqlLite - varchar

For text fields, the maximum length needed to store IPv6 addresses is 45 characters, so it looks like this is taken into account for MySql, SqlServer, and SqlLite. Additionally, the inet field in Postgres handles both IPv4 and IPv6 fields.

Considering all this, I'd say it is safe to assume the ipAddress() method will create a field that can handle IPv6 in any database.

patricus
  • 59,488
  • 15
  • 143
  • 145