I am currently implementing search functionality in my Laravel 5 application. Right now I have the following code:
$terms = implode("* ", explode(" ", trim($query)." "));
$sql = "MATCH(title, content) AGAINST('".$terms."' IN BOOLEAN MODE)";
$results = Post::whereRaw($sql);
Where $query
is a user-supplied string. The code takes (space-separated) keywords from the users and runs a full text search on them. The problem is that a simple '
can break the SQL query and allows for SQL injection. What would be the best way to prevent this?