Jinto Antony
Jinto Antony

Reputation: 468

Laravel eloquent using null as column value on select?

How to use null as column value in the Laravel eloquent like in the following query (The below query is not working and its for an example).

$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',
        ]);

Here, 'null as voucher_no' is not working. How to use null inside this query.

Upvotes: 2

Views: 6334

Answers (1)

Blue
Blue

Reputation: 22921

Use DB::raw around your columns, which will force laravel to pass the column name as is:

$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'),
    ]);

Upvotes: 10

Related Questions