mvnobrega's avatar

Normalize accents in where

I have the following query:

				$hospedagens = Hospedagem::whereNotNull('hotel_info.prices.price')
                    ->whereNotNull('hotel_info.about.description')
                    ->where('hotel_info.about.sub_descriptions', '!=', null)
                    ->orderBy('hospedagem.userRatingCount', 'desc')
                    ->where(function($q) use ($searchTerms) {
                        foreach ($searchTerms as $term) {
                            $q->where('hospedagem.displayName.text', 'LIKE', "%{$term}%");
                        }
                    }) ->get()

I need to make sure that the search term and the search object are all without accents. One obvious way I tried was the following:

foreach ($searchTerms as $term) {
				$normalize = Str::ascii($term);
              $q->whereRaw(Str::ascii('hospedagem.displayName.text'), 'LIKE', "%{$normalize}%");
 }

But it doesn't work at all. I've asked the AI ​​for help several times, and they give me several similar solutions that use whereRaw, but all I have to do is put in whereRaw() and I get the error:

 "message": "foreach() argument must be of type array|object, string given",
    "exception": "ErrorException",
    "file": "C:\\xampp\\htdocs\\serragaucha\\vendor\\mongodb\\laravel-mongodb\\src\\Query\\Builder.php",
    "line": 1238,
    "trace": [
        {
            "file": "C:\\xampp\\htdocs\\serragaucha\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Bootstrap\\HandleExceptions.php",
            "line": 255,
            "function": "handleError",
            "class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
            "type": "->"
        },

I'm using mongodb. I've been stuck on this for hours, I've tried a little bit of everything, with the help of AI, and I just can't solve it at all, it seems like something so simple, but I'm stuck on this and would like some help.

0 likes
1 reply

Please or to participate in this conversation.