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
JSON in Databases can't be used because Eloquent doesn't support the data types #13232
Comments
This issue is handled in 5.3 by using the correct PDO parameter type, I didn't submit the fix to 5.2 because I thought it might be breaking, it's not breaking for MySQL and Postgres but I'm not sure about other engines. |
That is not true. It does work for integers, right, but it does not for boolean or floating point numbers. So the issue is indeed still not resolved. To check it out yourself, do the following: Make a new laravel app, then use // Create the table
Schema::create('tests', function($table){
$table->string('name');
$table->json('attr');
}); // Insert the test data
DB::table('tests')->insert([
['name' => 'a', 'attr' => json_encode(['a' => 1, 'b' => 3.141592654, 'c' => 'foo'])],
['name' => 'b', 'attr' => json_encode(['a' => 2, 'b' => 1.718281828, 'c' => 'bar'])],
]); // Test it
DB::table('tests')->select('name', 'attr')->where('attr->b', 1.718281828)->get(); Instead of giving back the row with the
|
A patch was added to handle floats, however for booleans there's something with the PDO I agree but quoting Taylor here #13166 (comment)
I think we'll need to handle the matter using the PDO somehow, or just settle for strings and integers and hint that in the docs for now. |
Just ignoring the issue is not gonna make it go away. It is in fact not doable with the PDO, so why wouldn't we fix it? I see we can't build our own PDO. So why not accept the fix in #13166 or come up with another solution? |
@GrahamCampbell please reopen this issue as nothing has been resolved here, while the issue is a real issue |
@taylorotwell I'd like to hear your opinion about that. |
@themsaid What do you make of this please? |
DB::table('table')->where('attr->a', true)->first() It'll return null while it should return the existing record, In my opinion the only way we can deal with this is by manually injecting DB::table('table')->where('attr->a', DB::raw('true'))->first() Regarding doubles, it's handled in 5.3 now. So strings, integers, and doubles are ok, bools no. |
I kinda got the vibe that you guys don't give anything on my opinion, but still this is not where it should end. Why not let the fix from #13166 do the bool work for you instead of having to manually use DB::raw all the time, despite it being very counter-intuitive. |
@sebwas I'm saying that your fix for bools is the only way in my opinion, for doubles it can be handled by PDO::PARAM_INT but for bools, your fix is the only possible solution AFAIK. |
@sebwas |
@jaketoolson I'm perfectly aware of that. The thing is that you can get around that, which is where laravel comes in. |
@sebwas This issue can be closed? |
Yes, was fixed. |
Supposed you have a column
attr
in your table. Supposed your table has these entries:Now using Eloquent to get the row where the value of
a
is 1, you would have useDB::raw(1)
as the value in the call towhere
like this:The text was updated successfully, but these errors were encountered: