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

dink's avatar
Level 1

Eloquent query by first letter only

Hi All! I need to query my db records by a starting first letter or number only, nothing more. In my SearchController, I have the following, but it returns all records containing the given term in the title. Is there a way to do this? Thanks.

// SearchController.php

  public function browse($term)
  {
    $items = Item::query()
      ->with(['image'])
      ->where('title', 'LIKE', "%{$term}%")
      ->paginate(12);

    return view('items.browse', compact(['items', 'term']));
  }
0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122
    $items = Item::query()
      ->with(['image'])
      ->where('title', 'LIKE', "{$term}%")
      ->paginate(12);

returns all records that have title starting with $term

2 likes
dink's avatar
Level 1

@Snapey Well, that couldn't be any easier. I feel ashamed. Thank you so much!

Please or to participate in this conversation.