laravel 4将ip地址保存到模型中 [英] laravel 4 saving ip address to model

查看:177
本文介绍了laravel 4将ip地址保存到模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Laravel 4将用户IP地址保存到我的数据库。
我发现以下函数返回一个字符串

I'm trying to save a user ip address to my database using Laravel 4. I found the following function which returns a string

Request::getClientIp()

我如何将其存储在我的模型中?只是一个字符串或是否有更有效的方式?

How would I store this in my model? Just a string or is there a more efficient way?

$table->string('ip_address');


推荐答案

选项1:使用VARCHAR(45)专栏

考虑另一个SO问题中的讨论 IPv6地址的文本表示的最大长度?包含IPv4隧道功能时IPv6的最大长度为45。

Considering the discussion in another SO question Maximum length of the textual representation of an IPv6 address?, the maximum length of IPv6 is 45 when including the IPv4 tunneling feature.

因此,更安全的迁移命令将是:

Thus, a safer migration command would be:

$table->string('ip_address', 45);

优点:


  1. 该列是人类可读的。设置值或查询要查看的行时无需转换。

缺点:


  1. 它使用的空间比选项2多,实际上几乎是后者的3倍。但除非你计划有数百万行,否则我不会太担心。






选项2:使用BLOB列

由于@euantorano提供了指向存储在mysql数据库中的IP地址,您可以将IP存储为二进制文件以节省一些空间。

As @euantorano provided the link to IP address storing in mysql database, you may store the IP as binary to save some space.

最简单的答案是使用:

$table->binary('ip_address');

优点:


  1. 以二进制形式存储IP地址将为您节省一些空间。

缺点:


  1. 您需要先使用PHP的 inet_pton()。该列不能直接读取,因为它以二进制格式存储。如果尝试直接查询,您将看到奇怪的字符或空白。您可能希望查看我在下面的选项3中存储和检索IP地址的方式。

  1. You will need to convert the IP address string to binary first using something like PHP's inet_pton(). The column will not be directly readable since it is stored in binary format. You will see weird characters or blank if tried to query it out directly. You may want to look at my way to store and retrieve the IP address in Option 3 below.

Laravel中的查询构建器,尽管该方法被称为二进制,实际上会为您创建一个BLOB专栏 BLOB存储在表格之外超出行缓冲区,其中可能意味着性能较低。并且没有理由不使用BINARY列类型,因为我们知道IP地址不需要那么长的BLOB。

The query builder in Laravel, despite the method being called binary, will actually create a BLOB column for you. BLOB is stored off the table, out of the row buffer, which possibly means a lower performance. And there really isn't a reason not to use BINARY column type since we know IP addresses aren't that long for BLOB to be necessary.






选项3:使用VARBINARY(16)列

Laravel's查询构建器为选项2中的示例生成BLOB列。如果您使用的是MySQL,则需要使用VARBINARY(16)而不是BLOB以获得更好的性能。

Laravel's query builder produces a BLOB column for the example in Option 2. If you are using MySQL, you will want to use VARBINARY(16) instead of BLOB for better performance.

迁移脚本:

class CreateMyLogsTable extends Migration {

    public function up()
    {
        Schema::create('my_logs', function(Blueprint $table) {
            $table->increments('id');
        });

        DB::statement('ALTER TABLE `my_logs` ADD `ip_address` VARBINARY(16)');
    }

    public function down()
    {
        DB::statement('ALTER TABLE `my_logs` DROP COLUMN `ip_address`');

        Schema::drop('my_logs');
    }
}

显然上面唯一重要的部分是DB ::声明(...)。我们需要使用原始查询作为 Taylor Otwell建议。随意以你的方式创建表的其余部分。

Obviously the only important part above is the DB::statement(...). We need to use raw queries as Taylor Otwell suggested. Feel free to create the rest of the table your way.

从这里你可以使用PHP的 inet_pton() inet_ntop()将IP地址字符串转换为二进制,反之亦然。

From here you can use PHP's inet_pton() and inet_ntop() to convert the IP address strings to binary and vice versa.

优点:


  1. 与选项1相比节省空间

  2. 与选项2相比,DB性能更佳

缺点:


  1. 与选项2一样,您需要在二进制和人类可读的字符串之间来回手动转换,或者使用Eloquent模型和一对自定义访问器/ mutator,我将在下面演示。






额外信用:添加自定义Eloquent accessor / mutator(可选):

这里是我发现Eloquent非常有用的地方。您可以将自己的访问者/变异者设置为您的Eloquent模型,您可以照常通过模型的实例变量获取/设置。

Here is where I find Eloquent really useful. You can set your own accessor/mutator to your Eloquent model and you can get/set via your model's instance variable as usual.

class MyLog extends Eloquent {

    public $timestamps = false;

    public function getIpAddressAttribute($value)
    {
        return inet_ntop($value);
    }

    public function setIpAddressAttribute($value)
    {
        $this->attributes['ip_address'] = inet_pton($value);
    }
}

现在,如果你这样做:

$log = new MyLog;
$log->ip_address = '192.168.0.1';
$log->save();

IP地址将正确保存为二进制文件。您可以这样做:

The IP address will be saved as binary correctly. And you can do:

$log = MyLog::find(1);
echo $log->ip_address;

它将回显192.168.0.1。非常有用!

And it will echo out 192.168.0.1. Very useful!

这篇关于laravel 4将ip地址保存到模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆