0

I'm trying to make a search engine for my site. It works unless there are accents in a word.

For example, if I have the title: "Musée Baudouin", if I write "Musee Baudouin" or "Musée Baudouin", it does not display anything. If I write just "Baudouin", it's work.

I can't find my articles if they have accents in the title.

How to do ?

public function searchNav(Request $request)
  {
    $search = $request->get('search');

    $buildings = Building::where('title', 'LIKE', '%' . $search . '%')->orWhere('city', 'LIKE', '%' . $search . '%')->published()->get();
    $events = Event::where('title', 'LIKE', '%' . $search . '%')->orWhere('city', 'LIKE', '%' . $search . '%')->published()->get();

    // Create a new collection and merge elements
    $all = new Collection();
    $all = $all->merge($buildings);
    $all = $all->merge($events);
    $all = $all->sortByDesc('updated_at');
    $this->data['items'] = $all->all();

    return view('pages.search-nav-results', $this->data);
  }

Thanks

Jeremy
  • 1,756
  • 3
  • 21
  • 45

1 Answers1

2

This sounds like an encoding problem, meaning that your current encoding does not support accented characters. Here is a way to create a MySQL database using a UTF-8 encoding, which certainly would support the accented characters in your example:

CREATE DATABASE yourdb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

This would fix the MySQL side of things, assuming you are not already using an encoding which supports those accented characters. On the PHP side, it is a bit more complicated, but there you also need to make sure that the proper encoding is being supported. I refer you to a good blog site which covers the PHP details.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360