This episode is for members only

Sign up to access "Dropdown Autocomplete Search Anything in Laravel" right now.

Get started
Already a member? Sign in to continue
07. Clicking through on results


So everything is looking good here when we type stuff out. But there is one issue. We can't click these to actually go anywhere. At the moment, when we click them,
we just end up back on the same page because we have an empty anchor with no href. So what we're going to do now is take a look at how we can link these up to a page.
Now, we don't have any pages to show this data just yet. But let's go over to our web routes. Just create one out here. We're not going to do this with inertia
because I don't want this to be inertia specific. So I'm going to go ahead and create out a get route here for both our users and our courses. So this will be something like users.
And then we'll use route model binding to grab the user. And then inside of here, let's grab the user that we get through with route model binding. And let's just die dump on that user
as we saw from the introduction. So we're just keeping this really, really simple. But of course, you can build out the pages for the actual results themselves.
So we're going to do the same thing here for our courses. And of course, we'll get a course in here. We'll get a course in here. And we'll call that course.
And there we go. Now, we're going to name these so we can easily reference them. So I'm going to call this just, for example.
And we can do the same thing for our courses as well. Now, the thing about this is, if we were to, over in our search, do something like this,
so I don't know which one this is. It's the users. We could do something like users slash, and then we could take the item ID, which is the user ID.
And this would work. So let's just have a real quick look at this. If I click on one of these, we go through to the right page.
So this is working as we would expect. But the only problem is, if this URL changes, we kind of want this to be referenced by name. Now, we can't do this here
because we can't necessarily reference something by name within our component. We can do that with inertia, but again, we're going to look at how we can do this
and index the URL instead. So we can just reference this directly in here, but you can do really whatever you like. Let's go over to our user model,
and we're going to add the URL for this into here. So we're going to use our root helper. We're going to say, and this URL will link to this model
with root model bindings, so we can just use this. And we can do the same thing over in our course as well. So let's go down here, put this in here, and say, and again, reference this.
So now what we can do is re-index everything. So let's use Scout to import our user, and let's do the same for our course as well. And we'll just verify that this data is inside of here.
Sure enough, it is, and it links through to the right place. So now we can just reference the URL. Now, there is one problem with this solution,
and that is if your URLs change for your courses or your users or any other data you're indexing, you are going to have to re-index all of this data. So if you are using Inertia,
you could just link through using the root helper with Ziggy. If you're an Inertia developer, you'll know what that is, but otherwise, this solution is probably the best
because it just allows you to define this in one place, and if it changes, you just have one place to change it rather than fiddling around with your code. So in here now, we can just go ahead
and add this into this href. So we're gonna say item URL, and we can do the same thing for our course as well. So let's just close this one off, open this one out,
and we'll go down to here and say item URL. So we've got a common property that exists for all of the things that we are searching on. So let's try this out.
I'm gonna click on one of these users. Works nicely. And let's go ahead and choose one of these courses, and again, goes through to the correct place.
So this is a good strategy if you're just building out a simple view component within an app. If you're using Inertia,
you can use your root helper like you normally would to go through to this particular location. But I think this is absolutely fine. It's very rare that your URLs will change.
So as long as they're in there, you can just really quickly reference them directly from your search results. And then of course, link the user through to the right page.
12 episodes1 hr 3 mins


Using the power of Laravel Scout and Meilisearch, let’s build an instant dropdown search in your Laravel apps.

We’ll start with the basics of indexing data, then use the JavaScript autocomplete library to instantly show results as the user types — even multiple sources at the same time!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.