laravel-json-api / eloquent

Serialize Eloquent models to JSON API resources
MIT License
12 stars 15 forks source link

Feature/wherelike filter #28

Open tommie1001 opened 1 year ago

tommie1001 commented 1 year ago

I also had this one in my project, say an pull reqeust lately which was open for 5 months or so. Let me know if this is a filter you would like to have in jsonapi, usecase would be WhereLike::make('column'), request: /api/v1/endpoint?filter[column]=foo.

lindyhopchris commented 1 year ago

Sure, will take a look. I have a where-like filter in my project, so will be interesting to compare and see what we can add to the package.

lindyhopchris commented 1 year ago

Hey. Sorry, finally had a look at this.

Can you provide some info on what escapeLike is doing? With some examples would be good.

Two things come to mind:

  1. This is doing %term%, but the developer might want it to only do term% or %term, so we might need to allow that to be customisable.
  2. We probably want to allow ILIKE as an alternative, maybe via a caseInsensitive() function or something like that?
tommie1001 commented 1 year ago

Hi @lindyhopchris sorry for the long awaited reply but here i am!

1 I will add something so the user can decide weither they want the wildcard in front of the like, or at the end, or just both. And I will add an ILIKE via an new caseInsensitive method so the developer has also some freedom with this. Expect an update for this pull request today!

2 EscapeLike is used to escape the special characters that are also used in a like query. I asked chatGPT to give me a nice example so I don't have to put an effort in writing this al down:



$value: The first parameter is the string that needs to be escaped. This string may contain special characters like "%", "_" or the escape character itself (which is specified by the second parameter).

$char: The second parameter is an optional parameter that specifies the escape character. If this parameter is not specified, the default escape character is the backslash ("").

The method uses the str_replace() function to replace the special characters in the $value string with their escaped counterparts. Specifically, it replaces the escape character and the "%" and "_" characters with the escape character followed by the same character.

For example, if the escape character is a backslash ("") and the $value parameter is "hello%world_", the method will return "hello\%world\_". This escaped string can then be used safely in a SQL LIKE query without any unexpected behavior.

Overall, this method is a useful utility function for safely escaping special characters in SQL queries, specifically in the context of the LIKE operator.```
MeiKatz commented 1 year ago

I am not sure if the WhereLike filter is the right place for this, but maybe the filter could also support matching of multiple search pattern, kind of Where and WhereIn combined, but for the LIKE operator.

Anyway, if there is a WhereLike, there should also be a WhereNotLike filter.