【问题标题】:Laravel eloquent using null as column value on select?Laravel 雄辩地在选择时使用 null 作为列值?
【发布时间】:2018-08-12 05:20:49
【问题描述】:

如何在 Laravel eloquent 中使用 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 coupon_no' 不起作用。如何在此查询中使用 null。

【问题讨论】:

    标签: php sql laravel-5


    【解决方案1】:

    在你的列周围使用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'),
        ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-27
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      相关资源
      最近更新 更多