How to return 0 instead of null in laravel?

by ryan.murray , in category: PHP General , a year ago

How to return 0 instead of null in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by giovanny.lueilwitz , a year ago

@ryan.murray 

In Laravel, you can use the ?? operator to return a default value if a variable is null.


Here is an example of how you can return 0 instead of null:

1
2
3
4
5
$value = null;

$result = $value ?? 0;

return $result;


In this example, if the $value variable is null, the $result variable will be set to 0 using the ?? operator. You can adjust the default value to whatever you need.

Related Threads:

How to return 0 instead of null in postgresql?
How to return null results in sparql?
How to display 0 if no record in laravel?
How to use mysql instead of mariadb in symfony 5?
How to skip null and execute query in laravel?
How to return response in laravel from trait?