12
votes

there is no way to make such a query(with the binding) using the Laravel query builder right now:

SELECT * FROM `posts` WHERE MATCH( `title`, `description` AGAINST( 'bar' IN BOOLEAN MODE)) ORDER BY (MATCH( 'title' AGAINST( 'bar' )) DESC;

this will order the results by relevance, If we had(which we don't now!) orderByRaw then the above query would be:

Post::whereRaw("MATCH( `title`, `description` AGAINST( ? IN BOOLEAN MODE))", array('bar'))->orderByRaw("(MATCH( 'title' AGAINST( ? )) DESC", array('bar'))->get();

I opened this issue but It didn't get anywhere: https://github.com/laravel/framework/issues/2134

any suggestion?

1

1 Answers

28
votes

Using Eloquent you can do the following:

$match = "
    match (
        `title`,
        `description`
    ) against (
        ?
        IN BOOLEAN MODE
    )";

$against = 'bar';

$sql->whereRaw($match, array($against));
$sql->orderByRaw($match . " DESC", array($against));

The same would work with the Query Builder, but then you have to do a little rewriting.