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

Nicolath3cat's avatar

Laravel Query builder puts whitespace on my string

Hi, I'm new on this forum.

I have a problem with a line of the query:

whereBetween("CdrRaw.datetime", [$DataI,$DataF])

This attempt threw an error and the query debug displays:

where 'CdrRaw'.'datetime' between ? and ?

So I tried to make it Raw with

->whereRaw("CdrRaw.datetime between $DataI AND $DataF")

And here I could see the problem: the query builder puts some whitespaces in my date string even if $DateI and $DateF are simple date strings whithout any whitespace!

where CdrRaw.datetime between 2022 -03 -06 AND 2022 -03 -07

I also tried removing the unwanted chars with "str_replace()" but without any result, what can I do?

Thanks in advance for your time!

0 likes
9 replies
Nakov's avatar

What are these $DataI and $DataF are you formatting them? Try passing just Carbon instances and let Laravel do the rest.

Maybe this if you are passing them as Strings.

whereBetween("CdrRaw.datetime", [Carbon::parse($DataI), Carbon::parse($DataF)])
Nicolath3cat's avatar

@Nakov This is just for debug purpose, after those two dates should be retrieved by a form. At the moment $DateI and $DateF are declared as follows:

        $DataI = date('Y-m-d', strtotime("yesterday"));
        $DataF = date('Y-m-d');

EDIT: Just tried with Carbon but same result as my first try

where
  `CdrRaw`.`datetime` between ?
  and ?
Nakov's avatar

@Nicolath3cat and this:

whereBetween("CdrRaw.datetime", [today()->subDay(), today()])

?

And you didn't accidentally edited anything in the vendor folders for the whereBetween implementation right?

Sinnbeck's avatar

How do you know? There is no way of inspecting the query being run (except maybe using a profiler on the server or checking logs)

Nicolath3cat's avatar

@Sinnbeck I May not share links on my first day, I wanted to show you my artisan debug page, I think it is called Flare or Ignition

Oh, i forgot:

Environment information

Laravel 8.80.0

Laravel locale en

Laravel config cached: false

PHP version 7.4.3

Sinnbeck's avatar

@Nicolath3cat I assume you are talking about debugbar? That just outputs in what it thinks the query looks like. It isnt the actual query.

But I assume that the response you get from the database is wrong then?

Nicolath3cat's avatar

I just understood an important thing:

In my first attempt:

whereBetween("CdrRaw.datetime", [$DataI,$DataF])

with its result

where 'CdrRaw'.'datetime' between ? and ?

the "?" aren't things like "Unknown value", but they are placeholders of some variables. Under my query I could see the actual values of those placeholders and they were declared as follows

0
    2022-03-06 
1
    2022-03-07 

So I assume the error should be somewhere else... Anyway my initial error is "Trying to get property 'Name' of non-object"... so the query returned an empty collection I think...

Please or to participate in this conversation.