首页 > 解决方案 > Laravel - 如何在 LaravelChart 模型中添加 where 子句

问题描述

在我的 Laravel-5.8 中,我有以下代码:

   $chart_settings = [
        'chart_title'        => 'Users By Months',
        'chart_type'         => 'line',
        'report_type'        => 'group_by_date',
        'model'              => 'App\\User',
        'group_by_field'     => 'last_login_at',
        'group_by_period'    => 'month',
        'aggregate_function' => 'count',
        'filter_field'       => 'last_login_at',
        'column_class'       => 'col-md-12',
        'entries_number'     => '5',
    ];
    $chart = new LaravelChart($chart_settings);

如何将此 where 子句添加到上面的代码中('model' => 'App\User',)

where('hr_status', 0)->where('company_id', $userCompany)

谢谢

标签: laravel

解决方案


您可以使用where_raw来指定自定义 where 条件

$chart_settings = [
    'chart_title'        => 'Users By Months',
    'chart_type'         => 'line',
    'report_type'        => 'group_by_date',
    'model'              => 'App\\User',
    'group_by_field'     => 'last_login_at',
    'group_by_period'    => 'month',
    'aggregate_function' => 'count',
    'filter_field'       => 'last_login_at',
    'column_class'       => 'col-md-12',
    'entries_number'     => '5',
    'where_raw'          => 'hr_status = 0 AND company_id ='.$userCompany
];
$chart = new LaravelChart($chart_settings);

推荐阅读