php Laravel 迁移数组类型(将数组存储在数据库列中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32954424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:18:05  来源:igfitidea点击:

Laravel migration array type (store array in database column)

phparrayslaraveltypeslaravel-5.1

提问by Zakaria Acharki

I want to store an array of integers in my table and I can't find any type that supports array in Documentation, any suggestion.

我想在我的表中存储一个整数数组,但在文档中找不到任何支持数组的类型,任何建议。

Migration:

移民:

public function up()
{
    Schema::create('pickups', function (Blueprint $table) {
        $table->increment('id');
        $table->boolean('default');
        $table->integer('shifts');  <<--------- HERE I want to store an array of integers
        $table->integer('status_id');

        $table->timestamps();
    });
}

回答by Bogdan

The arraydatatype is not present in all database systems and because Laravel's Schema Builder is database agnostic, it doesn't offer methods to create non-common datatype columns. So you have two options:

array数据类型并非存在于所有数据库系统中,并且因为 Laravel 的 Schema Builder 与数据库无关,它不提供创建非通用数据类型列的方法。所以你有两个选择:

1.Use a raw SQL statement to add the column, something like the statement below I think should work. Although I'm not sure if the Query Builder or Eloquent can handle these types of columns correctly:

1.使用原始 SQL 语句添加列,类似于下面我认为应该工作的语句。虽然我不确定 Query Builder 或 Eloquent 是否可以正确处理这些类型的列:

DB::statement('ALTER TABLE pickups ADD COLUMN shifts integer[]');

2.Use Eloquent's available workaround by using attribute casting. In your migration create the column as jsonlike so:

2.通过使用属性转换来使用 Eloquent 的可用解决方法。在您的迁移中创建列,json如下所示:

public function up()
{
    Schema::create('pickups', function (Blueprint $table) {
        $table->increment('id');
        $table->boolean('default');
        $table->json('shifts');
        $table->integer('status_id');

        $table->timestamps();
    });
}

Then you can setup your Pickupmodel (if you haven't done so already) and use the $castsproperty:

然后你可以设置你的Pickup模型(如果你还没有这样做)并使用$casts属性:

class Pickup extends Model
{
    protected $casts = [
        'shifts' => 'array'
    ];
}

This will let Eloquent know that when it fetches data from the database it will have to convert the shiftscolumn value to an array. This is only emulating an actual array, as at the database level the column is of type TEXTand the array is serialized. However when unserializing the column value, Eloquent returns an actual array of integers for you to use in your code. Below is an example use case:

这会让 Eloquent 知道当它从数据库中获取数据时,它必须将shifts列值转换为array. 这只是模拟一个实际的数组,因为在数据库级别,列是类型的TEXT,并且数组是序列化的。然而,当反序​​列化列值时,Eloquent 会返回一个实际的整数数组供您在代码中使用。下面是一个示例用例:

// Create a new Pickup entry
$pickup = App\Pickup::create([
    'default' => true,
    'shifts' => '[1, 5, 7]', // you can easily assign an actual integer array here
    'status_id' => 1
]);

Assuming the above generated an entry with idequal to 1when you later retrieve the entry:

假设上面生成的条目id等于1当您稍后检索条目时:

$pickup = App\Pickup::find(1);
dump($pickup->shifts);

The dump()from the code above will output an actual array of integers:

dump()从上方将输出整数的实际数组的代码:

array:3 [▼
  0 => 1
  1 => 5
  2 => 7
]