1

This short eloquent firstOrCreate function is not working on linux web server. I've tried in localhost on windows and its working fine but as you can see the exception below, query string is not encoded properly.

Running on Laravel 5.8 PHP 7.3 with MySQL 5.7

$search = SearchHistory::firstOrCreate([
                    'ip_address'    => request()->ip(),
                    'data'          => json_encode(request()->query())
                ], [
                    'user_id'       => auth()->id() ?? 0,
                    'user_agent'    => request()->userAgent()
                ]);

The formatted query should be like:

select * from `search_histories` where (`ip_address` = '127.0.0.1' and `data` = '{\"inc_surrounding\":\"on\",\"address\":\"Surry Hills, New South Wales, Australia\"}') limit 1

But i'm getting this error..

Illuminate \ Database \ QueryException (HY000)
SQLSTATE[HY000]: General error: 2036 (SQL: select * from `search_histories` where (`ip_address` = 27.34.72.242 and `data` = {"address":"Surry Hills, New South Wales, Australia","radius":"10","price":{"min":"100000","max":"800000"},"bedroom":"0","bathroom":"0","parking":"0","land_size":{"min":null,"max":null}}) limit 1)
  • You can usually ignore the encoding in these error messages. They are inserted via PDO, so as long as you're using the correct methods, it's correct. Have you run all of your migrations? – aynber Sep 09 '19 at 12:13
  • Try https://stackoverflow.com/questions/38864391/laravel-5-2-query-building-for-json-on-columns and https://stackoverflow.com/questions/49846442/laravel-5-6-mysql-5-7-cannot-select-the-new-json-type-field – aynber Sep 09 '19 at 12:15
  • Yes, all my migrations are up to date.. – Sanish Gurung Sep 09 '19 at 12:16
  • Okay, I got it solved, thanks. – Sanish Gurung Sep 09 '19 at 12:21

0 Answers0