如何在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。
答案 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'),
]);