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

Crazylife's avatar

How to match word from beginning until a space in a query to get result?

How to query a record where need to match word from beginning until a space?

For example, i have entered full keyword "tesco" then in my database consist of "tesco store". I would like to return this result.

If keyword "tes" entered, i will not show any record even there's partial string match with it.

I cannot use LIKE % % for it. Any method can achieve it?

0 likes
6 replies
Snapey's avatar

You can use spaces in the search as well as %

$term = 'tesco';

$model->where('field', 'like' , "% $term %")->get()`

Now matches whole word only?

Cobs's avatar

i suppose you can do this by exploding your string and use a WHERE OR block.


$keywords = explode(' ', 'tesco store');
$query->where(function($q) use($keywords){
    foreach($keywords as $keyword)
    {
        $q->orWhere('keyword','LIKE',$keyword);
    }
});
Crazylife's avatar

@SNAPEY - For example, my database stored multiple value such as tesco store, product tesco, A4 tesco paper, and so on.

When i entered tesco, only tesco store will be returned. I want the search from beginning of the string, so product tesco, A4 tesco paper will not be returned for my case.

Next example, when i entered "tesco s", tesco store will not be returned too, it must be fully match the word like "tesco store" only return result.

I am currently using this method

substr(name, 1, instr(name, ' ') - 1)

This will match tesco, but when entered tesco store, no result will be returned.

Snapey's avatar

If you want it to from the start of the string only, leave off the first %... or am I misunderstanding?

$term = 'tesco';

$model->where('field', 'like' , "$term %")->get()
Crazylife's avatar

@SNAPEY - Let's say if i have some value like "tesco store", "product tesco", "A4 tesco paper", and so on.

If i search by "tes", no result will be returned. It must enter a full word like "tesco" to return "tesco store". I no want to return other value like "product tesco", "A4 tesco paper".

At the moment. i am using this way result will not be displayed when search by "tesco store"

$model->where(DB::raw("substr(name, 1, instr(name, ' ') - 1)", 'like' , $term)->get()`

Expected result

$term = 'tesco'; // return "tesco store"
$term = 'tesco store'; //return "tesco store"
$term = 'tes'; // return nothing
$term = 'tesco s'; // return nothing
$term = 'te'; //return nothing
Snapey's avatar

And what I showed last will do that.

The search term is at the start. No wildcard precedes it, so the search term must come at the beginning of the text.

The search term is followed by a space. There must be a space between the search term and the rest of the text.

The space is followed by % this means anything can follow the space.

Why is that not correct?

You have one case where this will not work, when the word is identical (with no space, ie 'tesco'). You can add an additional check for this;

    $model
        ->where('name', 'like' , "$term %")
        -orWhere('name','like',$term)
        ->get()

Please or to participate in this conversation.