php - Laravel Text 不显示 True/False,而是 0/1

标签 php laravel

在 MySQL 数据库中,我使用 tinyint(1),因此验证字段将为 0 或 1。 我如何编辑下面的推力以使其能够显示 True 或 False 而不是 0 或 1?

namespace App\Thrust;

use BadChoice\Thrust\Resource;
use BadChoice\Thrust\Fields\Link;
use BadChoice\Thrust\Fields\Text;
use BadChoice\Thrust\Fields\Email;
use BadChoice\Thrust\Fields\Gravatar;

class Requester extends Resource
{
    public static $model        = \App\Requester::class;
    public static $search       = ['name', 'email'];
    public static $defaultSort  = 'tickets_count';
    public static $defaultOrder = 'DESC';
public function fields()
    {
        return [
            Text::make('validate', 'Validate'),
        ];
    }
}

最佳答案

您正在寻找的功能称为 Type Casting .

The meaning of type casting is to use the value of a variable with different data type. In other word typecasting is a way to utilize one data type variable into the different data type. Source

例子

$response = 1;

var_dump($response); // Output: int(1)
var_dump((bool)$response); // Output: bool(true)

另一个例子可能是当你构建一个函数并使用它时你正在获取 1/0 值,如果你想将它作为 boolean 类型返回可以在里面做。例如:

public function isValid((string) $query): boolean
{
    $result = some_check($query);
    return (boolean) $result;
}

其他可能的数据类型转换是:

(int), (integer) - cast to integer
(bool), (boolean) - cast to boolean
(float), (double), (real) - cast to float
(string) - cast to string
(array) - cast to array
(object) - cast to object

关于php - Laravel Text 不显示 True/False,而是 0/1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59764921/

相关文章:

php - 从 PHP 生成 Chrome .crx

php - 如何在 PHP 中对 bcmath 数字进行舍入/上限/下限?

jquery - 流明ajax帖子

Laravel Assets 与相对路径

php - 选择时将不存在的字段设置为值

php - 无法连接到 Dreamhost 的数据库

JavaScript:语法错误:参数列表后缺少 ) - 如何在 php 中正确转义 js?

php - 检查 rtmp-stream 是否正在运行并使用 PHP/Shell-Script/Ruby 启动 ffmpeg

php - Laravel:在自定义登录中集成 Throttle

php - 如何从 Laravel 4.2 中不同表中的类别值中查找类别名称?