Skip to content

[5.2] change whereDate method for Postgres #12341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2016
Merged

[5.2] change whereDate method for Postgres #12341

merged 1 commit into from
Feb 26, 2016

Conversation

davidavz
Copy link
Contributor

I just had a problem with no results for a query using this method.

Wouldn't it be better to cast the column instead of the value ?

@davidavz davidavz changed the title change whereDate method for Postgres [5.2] change whereDate method for Postgres Feb 17, 2016
@taylorotwell
Copy link
Member

Can you post the query that was causing problems so I can reproduce it?

@GrahamCampbell
Copy link
Member

Wouldn't it be better to cast the column instead of the value ?

@taylorotwell Sounds like it's not a "bug", just a refactor.

@davidavz
Copy link
Contributor Author

Here it is

public function scopeCurrentDay($query)
{
    return $query->whereDate('started_at', '=', Carbon::now()->toDateString());
}

started_at is a timestamp

@GrahamCampbell
Copy link
Member

Don't call ->toDateString().

@davidavz
Copy link
Contributor Author

It still doesn't work.
For all the other methods (whereDay, whereMonth etc.) we etract from the column, not the value.
Why is it not the same for whereDate ?

@davidavz
Copy link
Contributor Author

?

@GrahamCampbell
Copy link
Member

Not entirely sure.

@taylorotwell
Copy link
Member

I think one problem with this is people are going to pass a full Carbon object into this and expect the time to be respected. Whereas this ::date operator is going to cut-off the hours, minutes, and seconds.

taylorotwell added a commit that referenced this pull request Feb 26, 2016
[5.2] change whereDate method for Postgres
@taylorotwell taylorotwell merged commit 1b4fa34 into laravel:5.2 Feb 26, 2016
taylorotwell pushed a commit that referenced this pull request Mar 27, 2025
…lumns (#55159)

In #12341, the cast to `::date`
was added to `whereDate`.

This leads to an error with PostgreSQL when passing in json column selectors.
For example:
`->whereDate('result->created_at', DB::raw('NOW()'))` will throw a syntax error for type date.
The same is true for the `whereTime` function with its `::time` cast.

To fix this, we need to wrap the column identifier in parentheses, when they are json paths.

Current SQL output:
```SQL
select * from "users" where "result"->>'created_at'::date = NOW()
```

Correct SQL output with this commit:
```SQL
select * from "users" where ("result"->>'created_at')::date = NOW()
```
taylorotwell pushed a commit to illuminate/database that referenced this pull request Mar 27, 2025
…lumns (#55159)

In laravel/framework#12341, the cast to `::date`
was added to `whereDate`.

This leads to an error with PostgreSQL when passing in json column selectors.
For example:
`->whereDate('result->created_at', DB::raw('NOW()'))` will throw a syntax error for type date.
The same is true for the `whereTime` function with its `::time` cast.

To fix this, we need to wrap the column identifier in parentheses, when they are json paths.

Current SQL output:
```SQL
select * from "users" where "result"->>'created_at'::date = NOW()
```

Correct SQL output with this commit:
```SQL
select * from "users" where ("result"->>'created_at')::date = NOW()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants