Check with a search engine in laravel

1

I wanted to know if the query is well done Where to find the posts of a blog that contain a word "query":

    public function search(Request $request)
    {
      //guarda la palabra en $query

      $query = $request->input('query');

      //consulta si "$query" se encuentra dentro del contenido de algun mensaje

      $messages = Message::with('user')->where('content','LIKE',"%query%")->get();

      //retorna la vista "welcome.blade.php" con los resultados

      return view('welcome', [

        'messages' => $messages,

      ]);

    }

It does not return any post, but I have 2 with that word.

    
asked by MarianoV 19.09.2017 в 02:16
source

1 answer

2

You should simply concatenate the percent characters (%) to the $ query variable, as in any PHP text string:

$messages = Message::with('user')->where('content','LIKE','%' . $query . '%')->get();
    
answered by 19.09.2017 в 02:31