Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cshelswell's avatar

Is this full text search possible in eloquent?

I'm trying to build a query to do a full text search on products. I've been using Eloquent to build up the query based on a number of other factors such as pagination, order etc.

I'm not using full text every time so how I'd got it is roughly like this (if i was doing a full text):

public function getProducts($input['string'])
{
    $query = $this->model->query();

    if($fulltext)
    {
        $query = $this->searchProductsFulltext($input['string']);
    }

    $query->where('product_active', '=', 'y');
    $order = (isset($input['sort'])) ? $input['sort'] : 'order';

    return $query->orderBy($order, 'asc')->paginate(50);
}

public function searchProductsFulltext($string)
{
    return $this->model->whereRaw('match (title, description) against (? in boolean mode)', [$string]);
}

This works fine in terms of returning results just not the most relevant ones. So i've got a new query:

select product.*
, match(`title`) against('$string' in boolean mode) as relevance_1
, match(`description`) against('$string' in boolean mode) as relevance_2
from product
where match(`title`, `description`) against ('$string' in boolean mode)
order by (relevance_1 * 2) + relevance_2 desc;

This one works fine but just wondering if there's a way to keep this in eloquent or if I just have to do a raw query.

Thanks

0 likes
2 replies
cshelswell's avatar

@phildawson Thanks for your answer. I actually ended up finding this: https://github.com/jarektkaczyk/eloquence which works really really well (for me at least).

All I have to do now is use this for my full text function:

public function searchProductsFulltext($string)
 {
    return $this->model->search($string, ['title'=>20, 'description'=>10]);
}

And just weight the columns by importance.

3 likes

Please or to participate in this conversation.