Getting Started with Laravel & MVC
Hey friends, welcome back to BitFumes. I'm your host Sarthak, and you can also call me Ankur. This is the channel where we will talk about the Laravel, cool things, tips and tricks, and many things about the Laravel. So if you are new to this channel, please don't forget to subscribe. In this episode, we are going to learn about the MVC and how we can get started with the Laravel.
Actually, there is a request from our subscribers, Sophie from Kenya. She messaged me on Facebook, and he asked a question that, "I have already installed Laravel and tested with the localhost and it works perfect. Now how can I begin to code?" Okay so the question is actually very basic, how you can start in the Laravel. If you have learned how to install the Laravel or if you don't know how to install Laravel, you can check out my video on how to install Laravel 5.4 by clicking this card. Okay, so let me start it.
The View: Displaying Your Content
So firstly, I want to tell you what the MVC is. So for that I have Googled what the MVC is, I landed on this Wikipedia page, and in this page, you can see you can read, actually this is a system but don't worry I am not going to read this. So let's get on this picture. So that picture is here. So and this is just stands for Model, View, and Controller, MVC. So firstly, talk about view. What is view?
Okay, so we had installed Laravel here. So you can see there is an h1 tag, there is a link also. So all these things, actually the HTML part, is the view. So let's get the picture. You can see the doctype HTML, everything is HTML, HTML and HTML. So this HTML part is our view. Obviously, it is view, that means it is viewable from the user end. Okay, so we understand view. That means anything a user can see is a view. So where is this view situated? So we can go here, and when we go to our resources folder, inside resources, there is a view folder.
So if you want to learn about what is blade.php, what is Blade, check out this card so that you can learn about what the blade is. Okay, so welcome here, you can see that all the HTML code are written here along with something in curly braces also. Okay, these are the things we have get from our controller, or we can say from our view via controller, and we put all these things here in our view. Okay, so we understand what is view. Okay, anything that can be viewable or seen by the user.
The Controller: Handling Logic & Requests
Now it's time for the controller. What is controller? It is a unit or it is a section of the model where you can implement all the logic, and you can get all the requests from the user, and you accept that request. So let's see how we will use controller. Okay, so where is the controller situation situated? For that you have to go to your app folder, inside that there is a HTTP folder, and now there is a controller folder, and you can see there is a controller, but this is not your controller. So you have to make a controller. How can we make a controller?
For making controller we will go to our command prompt, we will type php artisan make:controller and the controller name is boomkin controller controller. Okay, controller is created. If you don't— so what do you think what I am doing, what I am typing here? So don't worry, just see what I am going to explain to you. You will get all these things in the videos of this series. Okay, you can watch all the videos and after that if you have any doubts, please comment me. Okay, so just watch and don't worry about all these things. So you can see our Home controller is there. And now what we will do in controller? In controller, we make a function and we name that function, okay, this function is index. So here I will just say return, return what? View, and view name is, let's say, welcome. And as we have made controller, so I have told you controller is just for your logic. So what the logic is here? Actually, there is no logic. We just say return a view. That means if you call this index function, then it will return a view.
Routing to a Controller
So how does index function be called? For that, you have to go to web.php, or where is that web.php? Web.php is inside your routes folder. And you can see web.php here. You can see that it is says that route, that means if there is a GET request, you know, I think what is GET request, and when there is a GET request and there is homepage, that means your base URL is called, then you will return a view. Instead of this, what I am going to do, I am going to say route, and when there is a GET request for the base URL, you have to go to HomeController and in the HomeController, which function you have to pass? You have to call that index function. Okay, so when there is a GET request on your base URL, then it will call HomeController, and inside HomeController, it will call index function. Okay, so when I go to my local work, I will hit refresh, and you can see there is no error because we are actually returning the view from our controller. So you can see we have just cleaned our web.php page.
Passing Data from Controller to View
Okay, now what about the logic? Let's say if you have a variable called sum, and in that sum, you have typed 5 + 6. Okay, let's see, let's return this and see what the result is. Now comment this, click refresh, and you can see 11. And we know all these maths again.
So now what I'm going to do is I am going to just get this variable on my view. And where is that view? It's inside resource and welcome.blade.php. I want to echo, I want to show this some variable and the value of that sum variable inside my view. Let's see how we will do. We will just say compact and here I will type sum. What this means? This means that I have passed this variable, oh we don't have to say dollar, you have to just say variable name. Okay, what this statement means? This statement says that you have passed this sum variable in your view. Okay, now this sum variable is accessible in your view also. So let's see how we can do. We can go to our view. After this Laravel, I will just echo the sum.
Okay, this sum variable is came from this controller. Okay, now click refresh, and you can see 11 is now. What actually we have done? We have created a logic, and the result of that logic is passed to the welcome.blade.php, that means inside our view. So as I have told you, view are simply for the viewer and you interact and interact with your website with your project and that interaction, that the data, you will process inside the controller and return back to that view.
The Role of the Model
Okay, so it's very much easy. Now comes for the model. What the model do? Model is just like your brain. Model is actually storing data inside the database, obviously. So how we can make model? Similarly, we have to write a command, and obviously it will say make:model. Model name is let's say Home. So here you can see now the Home model is created inside our app folder. After this you can use that model here, you can say Home, and now you can get data from your Home model by saying various commands. Okay? In this way, you can get the data from the database. You have got the idea what the MVC is, and the further you watch my videos, you will get more and more deeper knowledge how the things work.
MVC Recap & Conclusion
Okay, so let's recap what we have done in this episode. In this episode, we have learned about the view, that means the user interaction, that means the HTML part. Okay, after that, we have learned about controller. What the controller is? Controller is just a place where you will put all your logic and return the result of that logic to the view. Okay. And next is the model. And the last, we have learned about the model. Model is just like your brain. It will get the data from the controller, okay, and it will store that data to the database. And again, if you want that data, that data model, it gives that data to controller to make a logic. And obviously after that logic, the controller returns that data to view.
So actually this episode is just for the beginners who doesn't know anything about Laravel, anything about any system. So if you have learned from this episode, please give it a thumbs up, like this video, and share this video with your friend who just want to implement, just want to get over the Laravel, over the MVC systems. So if you have any doubt in this episode or in any other project, then please comment me on the comment section. You can follow me on Twitter, Facebook, and Instagram. Okay, so we will meet in the next episode where we will learn something new, something more interesting, something cool about the Laravel. Till then, just keep coding and don't forget to subscribe. Bye bye.