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
Playing
02. Fresh project and seeding the database

Transcript

00:00
Okay so if you are new to Laravel or you just want to follow along from scratch, we're going to get started by creating out a fresh Laravel project and indexing some data in the database. Now when we create out this new project with the command that you can see here on the screen, we are going
00:14
to use the Inertia starter kit. Now don't let this put you off. Inertia will use Vue on the front end so this is all built with Vue. It's the easiest way to do this within a Laravel application but we're not going to be using any Inertia specific functionality so don't worry about the Inertia stuff. If you are coding along and you're just working in Vue, everything is going
00:35
to work. So this just makes it really easy to just get started and play around with this but it will transfer to a pure Vue app if you need to. Okay so let's go ahead and run the command to create out a new project here. Again I'm going to use Laravel Breeze and we're going to come down here and we're going to use Vue with Inertia. So if you are following along that's what you need to
00:55
do. We can enable server-side rendering but we're not going to cover that just here because it is going to work anyway. So let's go ahead and hit through that and let's get this app created. Okay so we're going to go ahead and select which database we're going to use. We're just using MySQL here and we are just going to wait for that to finish installing and seed some data in the
01:15
database. Okay so now that our app is installed and we're inside of it we are going to go ahead and run our default migrations. I've already got a database set up here with the name that's the convention when we create a project. This is laravel underscore drop down search but when we migrate anywhere it will ask you if your database needs creating and it will create that for you. So that
01:37
is the users table created. We are going to seed a bunch of users but we also want another thing because remember we want two or more sources of data for this stuff. So I'm immediately just before we do anything with seeding going to go ahead and make out a new model and we're going to create a user model. We're going to create a migration of course so we can actually create our database
01:58
schema but we're also going to create a factory alongside of this as well not a user a course. We already have a user so we're just going to create a migration and a factory. We can of course create a seeder but we're just going to do this with tinker on the command line. Okay so let's go over to our create courses table. Now the data that we work with here with different searches can be
02:20
completely different. It doesn't matter if one doesn't have a title or you have loads of different columns in your database that are all different. When we index this it's going to search on all of these attributes or the ones that we configure. So for courses for example of course users don't have titles they have names we're going to have a string in here with a title and we could pretty
02:41
much just keep it at that. So we're going to search on the title here so a course has a title and that is it but of course add more or use the data you've already got. So I'm going to go ahead and migrate this and if we head over to our course model we can see that this already has the has factory trait attached which means we can go ahead and use a factory to seed out a bunch of data.
03:04
So in our database just here we've got our users and we've got our courses let's start with seeding out some users. Now with our user we already have a user factory because this is a little bit more complex so you can see here we've got the name email email verified etc etc. With our course factory we just open this up we don't have anything in here yet so let's go ahead and just add in a
03:26
title. So we're going to go ahead and use the fake helper we're going to use sentence and let's just say six or eight words for that sentence so when we get to seeding this it will give us a title with eight words in it. Okay how do we see this data? A couple of ways we could create a seeder or we could just do this on the command line since we're playing around. So we're going to go ahead
03:48
and say php artisan tinker that's going to boot us into a tinker session and we can use our user factory like this and then we can choose how many times we want to generate this and we're going to go quite high here we're going to go up to a thousand and then we're going to go ahead and use the create method so that will create a thousand users in the database for us. There we go
04:09
now we can do the same thing and we can choose our course model so let's go ahead and do that and again we're going to create a thousand of them and you might just need to reference the class directly like this and there we go. So if we hop over to our database we've got a thousand users and we've got a thousand courses to play around with which we can index and of course
04:28
then search on. Okay so we haven't even come over to the app yet so let's just give this a refresh we're going to be building all of this on the dashboard again you can put this absolutely anywhere you want we're just going to be building out a really simple view component which we can just put anywhere so let's go ahead and just register an account so we're over on our dashboard
04:48
and there we go so we're going to be putting the search just in here but of course you can add it anywhere in your app it doesn't matter. Okay so we are set up with a completely fresh app that uses view it does use inertia but do not worry about that and we have seeded a bunch of data that we can start indexing into a search engine.
12 episodes1 hr 3 mins

Overview

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!

Episode discussion

No comments, yet. Be the first!