Home > Blockchain >  Laravel: Null and Empty
Laravel: Null and Empty

Time:07-21

  1. At least one character, not one space, two spaces, not null.
  2. Null or only spaces

1- At least one character, not one space, two spaces, not null.
In MySQL:

SELECT * FROM orders 
WHERE invoice_code IS NOT NULL AND TRIM(invoice_code)<>''

How to do this in Eloquent?

$query->whereNotNull('invoice_code');
$query->whereNotEmpty('invoice_code');

This will fail.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'not_empty' in 'where clause' (SQL: select count(*) as aggregate from `orders` where `not_empty` = invoice_code

whereNotEmpty becomes not_empty

2- Null or only spaces

SELECT * FROM cart_orders 
WHERE invoice_code IS NULL or TRIM(invoice_code)=''

If I use whereRaw(), it's bad for sql injection issue, isn't it?

CodePudding user response:

You could use a simple where:

$query->where('invoice_code', '!=', '')

CodePudding user response:

If you are using null for empty values Model::whereNotNull('invoice_code');

If you are using an empty string Model::where('invoice_code' '<>', '');

  • Related