Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

somnathsah's avatar

Select statement set null column defualt value to 0

Hi,

I am creating a query and executing it in sqlserver. I want to set null column value to 0 in select statement using method like coalesce, isnull but it is throwing error. "can not access empty property".

any help will be much appreciated.

Thanks, Somnath

0 likes
2 replies
okawei's avatar

So, you can add an attribute mutator in the model if all you're looking to do is return zero instead of null. Not sure if this is a solution in your case but here's how you'd do it

public function getYourColumnNameAttribute($value){
    return $value !== null ? $value : 0;
}

Just add that method to your model.

somnathsah's avatar

@okawei I am not using model instance for querying. I am using DB::table() to do the query. for example I am creating a query like this.

DB::table('test')
->select(DB::raw('isnull(column, 0)'))->get();

So, isnull function is not working, My database is SQL server.

Please or to participate in this conversation.