Laravel在迁移中提供数据类型位/字节

时间:2021-12-05 16:24:04

Is there any database similiar to bit (byte) in laravel, could not find any in the documentation. (https://laravel.com/docs/5.1/migrations).

是否有类似于laravel中的bit(byte)的数据库,在文档中找不到任何数据库。 (https://laravel.com/docs/5.1/migrations)。

I try to do something like:

我尝试做类似的事情:

00000001 -> stands for something lets say playstation

00000010 -> stands for xbox

00000011 -> stands for both above

1 个解决方案

#1


2  

Instead of trying to use the BIT datatype, which would be a bit of a hassle to work with, you can just use an integer and bitwise operators instead, assuming you don't need more than 32 options for one field (bigint = 8 bytes = 32 bits).

您可以只使用整数和按位运算符,而不是尝试使用BIT数据类型,假设一个字段不需要超过32个选项(bigint = 8个字节) = 32位)。

As a very simple example, imagine you create the following enum class:

作为一个非常简单的示例,假设您创建了以下枚举类:

class Bitmask {
    const ATARI       = 1 << 0; // 00000001 (1)
    const NES         = 1 << 1; // 00000010 (2)
    const SNES        = 1 << 2; // 00000100 (4)
    const SEGA        = 1 << 3; // 00001000 (8)
    const PLAYSTATION = 1 << 4; // 00010000 (16)
    const XBOX        = 1 << 5; // 00100000 (32)
}

To set the field, all you need to do is add the bitmasks together (in this configuration, ORing (|) them is the same). If a user has an NES and a PLAYSTATION:

要设置该字段,您需要做的就是将位掩码一起添加(在此配置中,ORing(|)它们是相同的)。如果用户有NES和PLAYSTATION:

$user->systems = Bitmask::NES + Bitmask::PLAYSTATION;

To query, you would use the bitwise operators. So, if you wanted the users that have SEGAs:

要查询,您将使用按位运算符。所以,如果你想要拥有SEGAs的用户:

User::where('systems', '&', Bitmask::SEGA)->get();

If you wanted the users that have PLAYSTATIONs or XBOXes:

如果您想要拥有PLAYSTATION或XBOXes的用户:

User::where('systems', '&', Bitmask::PLAYSTATION | Bitmask::XBOX)->get();

The & operator will perform a bitwise AND operation between the integer field and the integer you pass in. If any of the bits match up, the & operator will return a value > 0, and the where clause will be truthy. If none of the bits match up, the & operator will return 0 and the where clause will be false.

&运算符将在整数字段和传入的整数之间执行按位AND运算。如果任何位匹配,&运算符将返回值> 0,并且where子句将是真实的。如果所有位都不匹配,则&运算符将返回0并且where子句将为false。

#1


2  

Instead of trying to use the BIT datatype, which would be a bit of a hassle to work with, you can just use an integer and bitwise operators instead, assuming you don't need more than 32 options for one field (bigint = 8 bytes = 32 bits).

您可以只使用整数和按位运算符,而不是尝试使用BIT数据类型,假设一个字段不需要超过32个选项(bigint = 8个字节) = 32位)。

As a very simple example, imagine you create the following enum class:

作为一个非常简单的示例,假设您创建了以下枚举类:

class Bitmask {
    const ATARI       = 1 << 0; // 00000001 (1)
    const NES         = 1 << 1; // 00000010 (2)
    const SNES        = 1 << 2; // 00000100 (4)
    const SEGA        = 1 << 3; // 00001000 (8)
    const PLAYSTATION = 1 << 4; // 00010000 (16)
    const XBOX        = 1 << 5; // 00100000 (32)
}

To set the field, all you need to do is add the bitmasks together (in this configuration, ORing (|) them is the same). If a user has an NES and a PLAYSTATION:

要设置该字段,您需要做的就是将位掩码一起添加(在此配置中,ORing(|)它们是相同的)。如果用户有NES和PLAYSTATION:

$user->systems = Bitmask::NES + Bitmask::PLAYSTATION;

To query, you would use the bitwise operators. So, if you wanted the users that have SEGAs:

要查询,您将使用按位运算符。所以,如果你想要拥有SEGAs的用户:

User::where('systems', '&', Bitmask::SEGA)->get();

If you wanted the users that have PLAYSTATIONs or XBOXes:

如果您想要拥有PLAYSTATION或XBOXes的用户:

User::where('systems', '&', Bitmask::PLAYSTATION | Bitmask::XBOX)->get();

The & operator will perform a bitwise AND operation between the integer field and the integer you pass in. If any of the bits match up, the & operator will return a value > 0, and the where clause will be truthy. If none of the bits match up, the & operator will return 0 and the where clause will be false.

&运算符将在整数字段和传入的整数之间执行按位AND运算。如果任何位匹配,&运算符将返回值> 0,并且where子句将是真实的。如果所有位都不匹配,则&运算符将返回0并且where子句将为false。