PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 6, 2022

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

 February 06, 2022     eloquent, laravel, php, php-carbon     No comments   

Issue

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


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.



Answered By - apokryfos
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing