I have a working query like that:
$users = User::whereNotNull('name')orWhereNotNull('phone')orWhereNotNull(email)->get()
My database datas look like that
id | name | phone | email
1 | Luc | null | null
2 | Bob | null | bob@g.com
3 | null | null | null
4 | Bil | +2121 | bil@g.com
With my precious query it will return lines with id: 1, 2 and 3. But I want to order them by number of columns with null, which mean the users with the most null datas. In my example it will return in this order: 3, 1, 2
Is it possible to do it with eloquent? Like with a withCount()
and orderBy()
?
You can try this:
$users = User::whereNotNull('name')
->orWhereNotNull('phone')
->orWhereNotNull('email')
->orderByRaw('ISNULL(name) + ISNULL(phone) + ISNULL(email) DESC')
->get();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.