【问题标题】:how can i count number of records in table where status = 1 using laravel?如何使用 laravel 计算状态 = 1 的表中的记录数?
【发布时间】:2020-09-15 06:31:25
【问题描述】:

我有 1 个表 具有 3 个条件 的命名状态,即completedpostponedcanceled。我想把这三个条件改成一个数字,例如finish = 1postponedcanceled = 0。我要问的是如何将数据转换为laravel(php)中的数字

【问题讨论】:

  • 你可以在模型中创建const

标签: php laravel model-view-controller


【解决方案1】:

假设您在模型状态中有它并且字段存储为布尔值,您可以执行类似的操作

class Status
{
    protected $casts = [
        'completed' => 'integer',
        'postponed' => 'integer',
        'canceled' => 'integer',
    ];
}

这将起作用,因为(int) true1 并且(int) false0。对于更通用的解决方案,您可以定义访问器,如下所述:https://laravel.com/docs/7.x/eloquent-mutators#defining-an-accessor

【讨论】:

    猜你喜欢
    • 2018-12-05
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多