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

bekaskaki's avatar

convert query builder to eloquent

Is there a way to transform this query builder to eloquent ?

$products = DB::select("SELECT *,
(CASE
WHEN datediff(expire_date,CURDATE()) > 0 then datediff(expire_date,CURDATE())
ELSE 'Expired'
END) as remaining from products");
0 likes
2 replies
bobbybouwmann's avatar
Level 88

You can do something like this

$products = Product::select([
    '*',
    DB::raw('(CASE WHEN datediff(expire_date,CURDATE()) > 0 then datediff(expire_date,CURDATE()) ELSE 'Expired' END) as remaining'))
])->get();

This should do the trick for you.

Please or to participate in this conversation.