https://github.com/askdkc/whereraw_laravel
https://github.com/askdkc/whereraw_laravel
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/askdkc/whereraw_laravel
- Owner: askdkc
- Created: 2022-05-29T10:36:54.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-05-12T08:52:39.000Z (about 1 year ago)
- Last Synced: 2025-05-12T10:00:46.160Z (about 1 year ago)
- Language: PHP
- Size: 400 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## This is demo code to show whereRaw optional array handling error
This project uses PostgreSQL and [PGroonga](https://pgroonga.github.io). Please install both to continue.
If you are using macOS, you can install PGroonga with brew.
```
% brew install pgroonga
```
## Install and Reproduce the error
Clone this repo and do following.
```
% createdb whereraw_laravel
% cp -p .env.example .env
% composer install
% php artisan key:generate
% php artisan migrate --seed
% php artisan serve
```
## Demo
Access http://127.0.0.1:8000
Click "Without Optional" -> Works fine.
Click "With Optional" -> SQLSTATE[08P01]: <>.
## Got 2 solutions for this
I got 2 solutions. One from PGroonga Collaborator and other from Laravel Community.
### PGroonga Way
I got this solution via [Twitter](https://twitter.com/ktou/status/1531114276207079426).
```
$query->whereRaw('jsonbdata &` (\'(paths @ "title" || paths @ "body") && query("string", \' || pgroonga_escape(?) || \')\')', ["cat alice"]);
```
Check out [this branch](https://github.com/askdkc/whereraw_laravel/tree/pgroonga-way).
### Laravel/PHP Way
I got this solution from Laravel [issue report](https://github.com/laravel/framework/issues/42557) which I posted.
```
$input = 'cat alice';
$query->whereRaw('jsonbdata &` ?', [
'(paths @ "title" || paths @ "body") && query("string", "'. addslashes($input) .'")'
]);
```
Check out [this branch](https://github.com/askdkc/whereraw_laravel/tree/php-laravel-way).