我的代码中有一个很大的问题。我试图返回0,如果想要的行,返回null(这意味着没有数据符合搜索条件)如何在Laravel中返回0而不是null
我的查询是:
$total7 = DB::table('agencies')
->join('users' , 'users.id' , '=' , 'agencies.user_id')
->join('user_addresses' , 'user_addresses.user_id' , '=' , 'users.id')
->join('cities' , 'cities.id' , '=' , 'user_addresses.city_id')
->join('country_states', 'country_states.id' , '=' , 'cities.state_id')
->select('agencies.id as agenID ','country_states.name as sName', DB::raw('count(agencies.id) as agencount') , 'agencies.created_at as agencreate')
->where ('country_states.name','=','Capital Governorate')
->whereNULL ('agencies.id')
->whereBetween('agencies.created_at' , ['2017-01-01' , '2017-03-31'])
->groupBy(DB::raw("MONTH(agencies.created_at)") , 'country_states.name')
->get();
这种说法应该返回0,0,2但现在它什么也不返回 我希望它搜索抛出指定的月份范围(3个月),并返回值如果存在或0如果没有值。 我试过IFNULL
但没有。 有什么帮助吗?
- > whereNULL('agencies.id') 我真的不理解这部分。如果ID是一个自动增量值,为什么你检查它是否为空?它永远不会是null –
@Rodrane我试图让任何参数,使我的声明工作,因为我想。如果本月没有该省的身份证,则返回零。 – imohd23
当你完成你的查询后,只需做简单的(!$ result)? –