palauaandsons / nova-tags-field

A tags field for Nova apps that uses cartalyst/tags
https://palauaandsons.com
MIT License
9 stars 4 forks source link

Disappearing Suggestions #1

Open nickpoulos opened 5 years ago

nickpoulos commented 5 years ago

Hey guys, great package. I was using Spatie's own Nova field that uses their tag manager under the hood, but did not offer suggestions, so I switched to this one. However I am having a small bug.

The suggestions keep disappearing anytime there is a second or third keystroke. When I type in 'R' the Restaurants suggestions pops up, but then when I type in the rest like 'Res' it disappears. Anybody else experiencing this?

ipalaus commented 5 years ago

Hi,

Suggestions do work on both packages, Spatie's and our own too. I'd suggest you to check the responses the APIs are returning in the Chrome Dev Tools. Or you could also just write a dump in this file https://github.com/palauaandsons/nova-tags-field/blob/master/src/Http/Controllers/TagsFieldController.php#L21 to debug what's happening.

Let me know if that helps.

nickpoulos commented 5 years ago

I know Spatie's underlying tag package offers suggestions, but I did not see that on their Nova tag field package? Maybe I did not look well enough.

Anyway, so the bug is - you are automatically lowercasing ONLY the input, which never allows it to match tags that start with a capital. Please see line 21 of TagsFieldController.php.

php $query->where('name', 'like', '%' . strtolower($request['filter']['containing']) . '%');

Updating the like operator to 'ilike' for case-insensitive fixes the issue I am seeing.

php $query->where('name', 'ilike', '%' . strtolower($request['filter']['containing']) . '%');

I would make a PR and all that but since it is a one-char fix might be better if you do? Although you may want to remove the strtolower altogether now, I believe adding the 'i' should accomplish what strtolower was attempting to do. Thanks though!

nickpoulos commented 5 years ago

So the bug is, you are automatically lowercasing ONLY the input, which never allows it to match tags that start with a capital. Please see line 21 of TagsFieldController.php.

php $query->where('name', 'like', '%' . strtolower($request['filter']['containing']) . '%');

Updating the like operator to 'ilike' for case-insensitive should fix your bug. Thanks though!

php $query->where('name', 'ilike', '%' . strtolower($request['filter']['containing']) . '%');

ipalaus commented 5 years ago

This will return you both Restaurant and restaurant, just tried.

select * from tags where name like '%res%'

Which database driver are you using? ILIKE it's not a MySQL specific term

nickpoulos commented 5 years ago

Ahhhhh, ok ok, I think I know what the issue is. Yes that is true, I am using Postgres, I forgot MySQL does not have an iLIKE. Besides the lack of iLike operator, the driver should not matter otherwise. Generally speaking, case sensitivity is defined by the column/table/database collation settings.

Ex UTF8_GENERAL_CI you would not need to do any lower/case insensitive tricks, it is done automatically, just like your example.

That could very well be the discrepancy in the results we are seeing. Because I get an empty result set when I run

select * from tags where name like '%res%'

In order to not have any of these issues, and to be database driver agnostic, maybe it would be best to change the query to something like:

$query->whereRaw('LOWER(`name`) LIKE ? ',['%' . trim(strtolower($request['filter']['containing'])) .'%']);