0

i have an eloquent model with url and data, and other standard laravel fields.

then i do:

        $cache = new \App\Cache();
        $cache->url = $this->url;
        $cache->data = json_encode($data);
        $cache->save();

when url have no query string, it works perfectly, but when url have query string, laravel generate wrong query such as:

original data:

$this->url = 'https://some-url.com?param1=val1&param2=val2';
$data = ['some' => 'long', 'json' => 'data'];

turns to:

insert into `cache` (`url`, `data`, `updated_at`, `created_at`) 
values (
    https://some-url.com{"some":"long","json":"data"}param1=val1&param2=val2, 
    2019-01-07 07:38:15, 
    ?, 
    ?
)

as you can see, the encoded json is put on the wrong place (on the ? of the query string)

what did i do wrong? or this is a bug?

note: i'm using mysql

update: removed the error string as it distracting to the main issue

20
  • What is the size of url column in database? Commented Jan 7, 2019 at 8:19
  • url is string (191), data is longtext, the json should go to data, not to url Commented Jan 7, 2019 at 8:20
  • I guess you have to increase size of url column. Try doing that. Commented Jan 7, 2019 at 8:22
  • too long is not the problem, problem is the placement of the json Commented Jan 7, 2019 at 8:22
  • 1
    This has nothing to do with length of the fields. It's because Laravel uses ? as placeholders for data in queries. Commented Jan 7, 2019 at 8:29

4 Answers 4

1

I believe you need to store the URL like that in DB

$url_string = $this->url;
$cache->url = '{$url_string}';

It will be work for URLs

Sign up to request clarification or add additional context in comments.

1 Comment

sorry, was a mistake, this do save the data, but the url is saved as just '{$url}'
0

I think you should type cast the url as string and then save it or use the standard create() method this should work. I have used in one of my projects with no issue.

Comments

0

You can use urlencode() function. Try save your url with encoding.

$cache->url = urlencode($this->url);

and later you can get it with urldecode() function.

1 Comment

might do this work around if there is no other options
0

turns out its a bug on the sql logger that produce wrong sql snipet. the main error is just that the field length does not fit the data length as @Sohel0415 suggested.

so the logger is misleading me to think that the generated query is wrong.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.