Laravel Jetstream Search Input

Adam Bailey • December 28, 2023

laravel vue


I recently needed to incorporate search functionality on a Laravel Jetstream application using Inertia. This post outlines how I did that.

What is Laravel Jetstream?

Laravel Jetstream:

Laravel Jetstream is a beautifully designed application starter kit for Laravel and provides the perfect starting point for your next Laravel application. Jetstream provides the implementation for your application's login, registration, email verification, two-factor authentication, session management, API via Laravel Sanctum, and optional team management features.

I really love using Laravel to develop my applications, and for me, this was a no-brainer. Jetstream comes packed full of features I won't have to implement or worry about. These features already come with tests, so I can ensure im not breaking them as I go. I can build whatever I want inside it and use the pre-implemented components as I go. I am able to move pretty fast inside this architecture.

I chose to use the Inertia stack. I really love using Vue.js so this was also a very easy decision for me. When you choose this stack, it also comes with a whole range of Vue components that you can reuse throughout building your app.

However, one component that it did not come with was a search input for searching models.

I'm going to assume you already have a working instance of Laravel Jetstream working locally, and we'll start from there.

The Basic Controller

Let's say you want to fetch a list of stories from the database. You have a route like this:

1Route::get('/stories', [StoryController::class, 'index'])->name('stories.index');

and a controller method iin StoryController called index which looks like this, to get your stories and render them via Inertia to a page in Vue:

1public function index(Request $request): Response
3 return Inertia::render('Stories', [
4 'stories' => Story::paginate(),
5 ]);

The Basic Vue Page

Then on the Vue side, you receive the stories from the backend via a stories prop to this basic template:

2 <app-layout>
3 <template #header> Stories </template>
5 <div class="flex justify-between items-center">
6 <div class="text-3xl">Stories</div>
7 </div>
9 <div v-if="">
10 <ul>
11 <li v-for="(story, index) in" :key="index">
12 <span>{{ story.title }}</span>
13 </li>
14 </ul>
15 </div>
17 <div v-else class="text-gray-500">No stories were found.</div>
18 </app-layout>
21<script setup>
22 import AppLayout from "@/Layouts/AppLayout.vue";
24 defineProps({
25 stories: {
26 type: Object,
27 default: () => ({}),
28 }
29 });

Nothing fancy here. It's just going to return 15 stories if you have them, and this doesn't even give you a way to paginate to the next page. But its enough to work with for the purposes of this blog post.

New Search Input Component

We're going to make a new Vue component, just put it in a Components directory.

Here's the entire component:

2 <div class="w-1/2 bg-white px-4 dark:bg-gray-800">
3 <label for="search" class="hidden">Search</label>
4 <input
5 id="search"
6 ref="searchRef"
7 v-model="search"
8 class="h-10 w-full cursor-pointer rounded-full border border-gray-500 bg-gray-100 px-4 pb-0 pt-px text-gray-700 outline-none transition focus:border-purple-400"
9 :class="{ 'transition-border': search }"
10 autocomplete="off"
11 name="search"
12 placeholder="Search"
13 type="search"
14 @keyup.esc="search = null"
15 />
16 </div>
19<script setup>
20 import { ref, watch } from 'vue';
21 import { Inertia } from '@inertiajs/inertia';
22 import { debounce } from 'lodash';
24 const props = defineProps({
25 routeName: {
26 type: String,
27 required: true,
28 },
29 });
31 let search = ref(null);
32 let sort = ref(null);
33 const searchRef = ref(null);
35 watch(search, () => {
36 if (search.value) {
37 searchMethod();
38 } else {
39 Inertia.get(route(props.routeName));
40 }
41 });
43 const searchMethod = debounce(() => {
44 Inertia.get(
45 route(props.routeName),
46 { search: search.value, sort: sort.value },
47 { preserveState: false }
48 );
49 }, 2000);

We have a:

Incorporate Vue Search Component

Use the new component in the header of your Stories page like so:

1<div class="flex justify-between items-center">
2 <div class="font-header text-3xl md:text-5xl">Stories</div>
+ <search-input route-name="stories.index" />

And into the script area:

1<script setup>
2 import AppLayout from "@/Layouts/AppLayout.vue";
3 // add below line to import component
+ import SearchInput from "@/Components/SearchInput.vue";
6 defineProps({
7 stories: {
8 type: Object,
9 default: () => ({}),
10 },
11 });

Set Up Backend to Accept and Return Search

Since we don't yet have anything in our controller to receive the search value, nor do we have the inertia prop available to us in vue, we need to make some changes to the StoriesController.index method.

Here is the updated controller with everything we need:

1public function index(Request $request): Response
3 // get our search value from the request here:
4 $search = $request->search;
6 $stories = Story::query()
7 // when we have a search value, see if it matches anything in the title or content of the story:
8 ->when($search,
9 fn ($query) => $query->where('title', 'LIKE', '%'.$search.'%')
10 ->orWhere('content', 'LIKE', '%'.$search.'%')
11 )
12 ->paginate();
14 return Inertia::render('Stories', [
15 'stories' => $stories,
16 // and return the search value as a page prop to inertia/vue.
17 // This is the value we watch in the data() property of the SearchInput.vue component.
18 'search' => $search,
19 ]);


At this point you should have a working search input in your page which automatically starts searching your model as you type into the input. Hopefully you are able to understand this concept enough to incorporate it into your application.

You should be able to reuse this component for any pages in your app that return an index route. You just simply change the routeName prop, then set up the backend logic to process the search and return its value:

1Route::get('/whatevers', [WhateverController::class, 'index'])->name('whatevers.index');
1// in WhateverController.index
2$search = $request->search;
4// query search LIKE %whatever blah blah%
5$whatevers = Whatever::query()->when($search, fn ($query) => $query->where('something', 'LIKE', '%'.$search.'%'));
7// return it all
8return Inertia::render('Whatevers', [
9 'whatevers' => $whatevers,
10 'search' => $search,
1<search-input route-name="whatevers.index" />

I have certainly been enjoying how fast I can code out my ideas using Laravel Jetstream as a starting point for an app.

Hopefully this adds some value to your experience!

You may also find my article outlining how to apply the SpeechRecognition API to this component useful. Please see VueJs Search Input With SpeechRecognition API.

Happy coding!

You might like other posts in