Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pass dynamic value from database to Carbon::now()->subMinutes()

I have a minutes integer column in the db,any way to make the below query work without an additional query? Laravel 8 if it matters.

Contest::latest()->where('created_at','>',  Carbon::now()->subMinutes('minutes')->toDateTimeString())->paginate(4)

;

Reponse ErrorException: A non-numeric value encountered in file

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You cannot do this without an additional query. Carbon is a PHP library and won’t work within the SQL query. However SQL does provide native functionality for what you need to do:

Contest::latest()
    ->where('created_at','>', DB::raw('NOW()-INTERVAL minutes MINUTE'))->paginate(4)

This should generally work though you may need to replace NOW() with whatever the DBMS provides if not using MySQL.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading