Introduction to Laravel
Hello all and welcome to this webinar on Laravel. In this session, we shall be discussing the fundamental school concepts and pretty much all you need to build projects in Laravel. We'll also understand various concepts of prototyping, databases, and API along with some Laravel specific projects.
Our attendees here are a well rounded representation of the globe. We have Mr. Neil Row here who has years of valuable experience in this field and he shall be taking us through this module. Welcome, Neil, over to you.
Hey, so welcome to the first part of a new series. This is the Laravel series and it's effectively going to teach you pretty much everything you need to know to get started building products with Laravel. So if you've never heard of Laravel or you've kind of heard of it but don't really know what it does, Laravel is a framework.
Why Use the Laravel Framework?
And a framework is essentially the starting code to building your project. So instead of you having to write out all of these functions and all of these methods and controllers and things, the same code for all your projects, Laravel is like this starter kit. It kind of includes almost everything you need. So it makes a brilliant force for prototyping or building projects really quickly because it really has been built for so many years and they've really built up this whole platform where everything you could really need to build a startup or to build a web app is already included.
So I'm talking like database stuff, so there's so much sort of stuff for relationships and handling those relationships. It does it really neatly rather than having to write out your own queries and handle them yourself. Things like sockets, if you want to use Socket.IO or Pusher, it's all built in. There's tons of API stuff. So if you want to take payments, again, there's kits for that, they're all built in. So you can use like Stripe or Braintree or PayPal really nicely. And on top of that, there's a site called Packagist, which is essentially a load of packages that you can include in your project. These come through a piece of software called Composer and these are packages built by other people, other organizations, which you can make use of. And Composer will keep these up to date and to freely handle it all for you. So for example, if you want to use the Facebook API, you can literally just import a package which will give you a load of functions in PHP that you can access really nicely. And if you've used Composer before, you'll be familiar with that. But in Laravel, there's tons of Laravel specific projects as well, and you'll see those at some point in the course.
Course Overview & Prerequisites
Anyways Laravel is currently in version 5.7 at the time I'm recording this. It doesn't change massively between versions. So if you're watching this a year later on we're on 5.9 or something, this should all still be very relevant. This course is really going to teach like the fundamentals rather than anything actually specific. It's going to teach you the fundamentals to get started and then go off and learn by yourself or by other means. It's really going to teach you like the core concepts of how Laravel works and how to just get set up with it, and those kind of things don't really change between versions, so there shouldn't be a problem.
So to get started with Laravel, you're going to need a program called Composer, as I say. Composer is the package manager, is the most common one for PHP. If you're on a Mac or Linux or a Unix system, what you got to do is copy and paste all of these lines into a terminal and it will install it all for you. It's really easy. And if you're on Windows, there is actually an installer somewhere on here. I think it's in Getting Started, you'll see here, yeah, install the composer-setup.exe and it will do it all for you. It's very simple to get set up. So I think most of you shouldn't have an issue.
Creating Your First Laravel Project
Once you've got Composer, it's really dead simple to install PHP. What you got to do is jump over to your terminal. So I'm using Visual Studio Code, which I'd highly recommend. It's got a built-in terminal. So you've got to jump over to your terminal and type in composer to launch the composer program, create-project, and then the project is called laravel/laravel, and it's named this because that's the sort of GitHub organization and then Laravel was the project name. And then after that you give it a name. So I'm going to give it social.
In this short course, we're going to be going through like a Twitter start. It will show all the fundamentals. So we'll be doing database stuff. I'll be showing your models, controllers, views, maybe a little bit of JavaScript, but this is not a JavaScript course, so I wouldn't count on that. This is purely a back-end course. So of course, it's going to show you how to get set up, all the fundamentals, and then from there you can go on and expand on this. So I'm going to let that install. It should just take a couple of minutes.
That's installed. You will see they're actually set up an application key. So this is like a unique hash, it's base64 encoded for your project. And that leads me into the .env file.
Configuring the Environment (.env) File
Now .env, if you're unfamiliar, is effectively an environment file. It really specifies the environment that this project is running. So you'll see things like the app URL, the app name, the database credentials, mail drivers, various stuff like that. You can also put things like API keys in here if you want to store them. And effectively, if you've got two environments, let's say you've got a staging environment for testing and you've got a production environment, you can have different database details, different app URLs, different API keys, blah, blah, blah. And these .env files are not synced in Git. So they're in the .gitignore file. So if you put this repository on two servers, they'll both have separate .env files. That's how we use it in my work, that's how pretty much everyone does it.
So first things first is to get it set up with some settings. The top ones don't really matter too much as long as you've got debug true for testing. That will just show you all your error messages pretty much. The DB connection, you'll need to have that. So you can add to have a MySQL instance. I'm not going to show that in this video because it's pretty basic to get set up, but you're going to need your login details in your port and blah, blah, blah. So my database, I've got it in a program called Navicat. You can also use Sequel Pro. My database is called there so I'm just going to paste that in here. My username is root and my password is also root. This will vary depending on your MySQL setup, if you're using like MAMP or LAMP or built-in MySQL or whatever, you just need to find out what your login details are. And then that's it for the environment file. That's our database setup.
Running the Development Server
So in order to test your project, you can run a command in the command line called php artisan serve. And that's literally going to start up Laravel's built-in server. And that failed because I need to change directory into the social folder because I'm currently in the Laravel folder, but I've also created a project inside of that. So I'm going to cd into social and php artisan serve again. And there we go, we've got Laravel development server started and that is the link. So I'm want to go over here and go to my localhost, and there you go. There is the home page. This is just what comes out of the box. You'll see it's just some pretty basic HTML and some CSS going on in there. Okay, if I actually go to any links, you just get a 404 because nothing's actually built yet. All right.
Database Migrations Explained
So the first thing I want to do is set up a login system. Now, if you normally use PHP or some other language, you'd have to, first of all, you got to create the table in your database. And then you've got to think about what hashing method you want to use. And then you've got to create the register form and then the login form, then the forgot password form. It's very tedious. And if you've ever worked on multiple side projects, you're probably used to building this exact same thing many times. And this can take you a couple of hours to build. So if you're doing that for lots of different side projects, it's totally unnecessary. Laravel does this for you. It thinks about security out of the box, so you don't have to. It's just an all-around nicer environment to use.
So to set up authentication, what we're going to do is php artisan migrate. And migrations are basically schemas. It is the script to build a database. So it's not MySQL, it's a script telling it how to build the database tables. And these are stored in database/migrations. And then the migration names are here. They've got a timestamp and then a name. So the first one here is create_users_table, and you'll see it's got a public function up, which is to create the table, and the public function down, is to reverse it, to drop it. So up will create a schema, which is effectively the table. The table name is users and then it's got all of these columns. So it's got an ID, which is incrementing, and it's got a bunch of strings, so password, email, name, a remember_token, which you'll see in the database, is effectively a token for if the user decides to remember themselves, and timestamps—these are created_at and updated_at—and Laravel handles those automatically. So this migration comes out the box, but you can make your own, which I'll show you. But this one is specifically for authentication. So I'll hit migrate.
Running Migrations and Debugging
So I got an error, which looks like it's because, yep, my login is incorrect for my database. So I'm going to just go check on that now. That's in the .env file. If you see anything like 'access denied for user', check your database settings. And it turns out the reason here why mine did not work is because my password is actually root. But again, that really varies based on however you set up your database. So I'm gonna go ahead now and run php artisan migrate again and see if that does the trick.
There we go. Migration table created successfully. So it creates a table that keeps track of the migrations, and then it migrates these two tables. So create users table and create the password reset table.
So back in my database now, if I go ahead and refresh, sure enough, we've got three tables. The users table is the one that we want right now. So we've got name, email, password, remember, created, and updated at. The benefit of doing this in your code rather than just making tables, it might not make sense for just one person. But when you've got a team of people, the fact that they can just run php artisan migrate to get their development environment set up, is so helpful. It means you can keep your database in your version control, whether that's Git or whatever. It just makes a lot more sense. You can also use things called seeders, which can populate the table with dummy data. So let's say you want to populate it with some dummy customers, you can do that too. And that can be really useful for getting other developers on board. But I'm not going to go into that in this because this is really just a getting started guide. Okay.
Scaffolding the Authentication System
We're now ready to really get set up with our authentication. So to do this, php artisan make:auth. And you can find all of this in the Laravel documentation. This command literally sets up all of the PHP functions, the views, the controllers, it resets them all up to set up a whole login system. This includes login and registration. So if I run that, Authentication scaffolding generated successfully. So as I say, this comes out of the box. And then I need to run php artisan serve to get my web server running again. When I refresh, you'll see login and register suddenly appear.
So we're going to go ahead and register. So I'm gonna register with some details. I'm going to go [email protected]. I'm going to set it a password. I'm going to forget password and password. And you're now logged in. So this is also by Laravel. As you can tell, we're only like 5, 10 minutes in and we've already got authentication, we've got our database set up, it's doing it all for us. We've already got like some scaffolding, so a logout button, etc. And we can log in again using [email protected] password, and we're in. It's also got some forgotten password scaffolding that's already done right out of the box. So it's really neat. It really saves you like many hours. If you're someone like me who comes up with side projects and then ditches them very quickly, this can be really useful because you can build things so quickly. It's great for prototyping, it's great for building projects quickly. I use this where I work and it saves us so much time.
Understanding the MVC Architecture
A couple more bits I want to round up in this video is the whole MVC architecture behind Laravel and behind pretty much any framework nowadays. MVC stands for Model-View-Controller, and these are the three main things that make up an application. So starting off with Models. Models are effectively your data stores. So in this case of a MySQL database, a model represents a table in the database and these models can be related to each other using relationships, just like you would use in normal MySQL using a join query. You can have multiple models, that's a collection, and then you would loop through a collection to retrieve individual models. So that's really it. So each model usually, doesn't always, but usually represents a table that's related to it. Model can also represent API lookups and all sorts of other things, but I'm not going to get into that, it's just too confusing. A model generally represents a table with a similar name.
The next part is Views. Now a view is literally pretty much as HTML. So it's deals with the outputting of data on the screen or in emails. So a view literally deals with outputting on the screen or emails and things like that. So most of the time, this is just HTML. But Laravel comes with something called Blade. Blade is the templating engine, and if I go on the documentation, you can see some examples there. So it's effectively HTML, but you can insert these smart little snippets. So you can have sections if you want a master page with the header and the footer and all your JavaScript, and then you can have the content separate. It means you've only got to ever edit one file. If you've done a regular old PHP, you probably included a master page but it gets really funny if you want to set titles and things like that. Laravel has really thought of everything, so you can't go wrong. You can also do things like if statements within the HTML, loops, so you can echo variables, components—came out in a few versions ago, that's quite recent—there's just so much stuff. Slotting components, there's really tons of stuff and really for most people, it's going to be perfect. It saves you so much time. If you've ever done regular PHP, you might also have done string replaces where you import an HTML file using file_get_contents and then you string replace various tags within that file. I know we used to do it where I work and it's a nightmare after a while. So Blade really kills it for you. You might have also used something like Smarty templates which are pretty good, but Blade is really awesome.
Anyway, the final bit is Controllers, and this is where the actual logic happens. So this is where you might process data, you might have to do calculations. Effectively, your controller is what links your models to your views. It really is what handles the logic and it goes and gets data from your model, as it pushes data to your models, and then it goes and outputs that view. It's really like the middleman of your project, and that's it for MVC really.
Laravel Routing and Middleware
One final thing I want to talk about in this episode before we actually get started is routes. So there's a file called web.php. By the way, using a text editor like VS Code is brilliant because of the search. When you've got Laravel with so many files, it's so handy. But web.php is in this folder called routes along with console, channels, and api. So what this file is is it's effectively a list, almost, of URLs on your site. So for example /home will take you to this controller, the HomeController, and the function or the method inside of it is called index, and then we also name that route so that we can retrieve it later. This last part is optional. You can also create a route like this, so Route:: and then either get or post. So I'm going to go to get and then you can have /test and then you can put an inline function in here or anonymous function, I think that's called, where you go echo 'whatever'. So you don't actually have to reference a controller and a function. You can also put a function right inside there. But generally for organization's sake, you want to do it like this. There's also tons of things like route groups, middleware, tons of other stuff to go with it. But that's going to be too much for this episode.
I'll quickly touch on middleware, though. Middleware is like a middleman between the base layer of your project and actually getting to those controllers. It's like a security guard standing at the door before your controllers and it checks you out and decides if you're logged in and then lets you in or pushes you away. So for example, if you've got a page that only authenticated users can go to, you might have a middleware that checks they're logged in, and if they're not, says 'go away', and if they are logged in it lets them come through to the controller. But yeah, I'm going to leave this video here. Thank you so much for watching and I hope to see you in the next one.
Conclusion & Next Steps
I hope you have enjoyed listening to this video. Please be kind enough to like it and you can comment any of your doubts and queries, and we will reply them at the earliest. Do look out for more videos in our playlist and subscribe to Edureka channel to learn more. Happy learning!
Outro Music
more happy learning