Laravel雄辩地在选择时使用空值作为列值?

时间:2018-08-12 05:20:50

标签: php sql laravel-5

如何在Laravel雄辩中将null用作列值,如以下查询中所示(以下查询不起作用及其示例)。

$query = \DB::table('invoices as inv')
        ->leftjoin('customer as c', 'c.id', '=', 'inv.customer_id')
        ->select([
            'inv.id',
            'inv.invoice_date',
            'inv.invoice_no',
            'null as voucher_no',
            'null as credit',
        ]);

在这里,“ null as voucher_no ”不起作用。如何在此查询中使用null。

1 个答案:

答案 0 :(得分:5)

在您的列周围使用DB::raw,这将迫使laravel按原样传递列名称:

$query = \DB::table('invoices as inv')
    ->leftjoin('customer as c', 'c.id', '=', 'inv.customer_id')
    ->select([
        'inv.id',
        'inv.invoice_date',
        'inv.invoice_no',
        DB::raw('null as voucher_no'),
        DB::raw('null as credit'),
    ]);