What to expect in the next generation of Laravel Forge. Read the blog post
Rewatch this lesson

Next video in… 10

Working with the database

What is MVC?

Getting Started with Laravel

What is MVC?
Understand the Model-View-Controller pattern that keeps Laravel code organized. Create your first controller and see how MVC separates concerns in web applications.

So what is MVC? Well, right now, your route in this web.php routes file—it's doing everything. It's handling the request, it's preparing data, it's returning a view. And it might not seem like it's doing that much, mostly because Laravel does all that for you in this Route facade that's getting this slash directory and returning a view. It's simple, but it's doing a lot under the hood.

As your application grows, you'll want a better way to organize things. And that's where the MVC model comes in. MVC stands for Model-View-Controller, and it's the pattern that keeps your code clean and your sanity intact.

The Restaurant Analogy

If we're going to take this into an analogy, let's think of a restaurant. While MVC is not necessarily the order that everything happens in terms of model and then view and then controller within Laravel, let's talk about the controller first.

Imagine a restaurant:

  • The Waiter (Controller): This is what you would think of as the waiter in a restaurant. It takes your order, it coordinates everything, and then it brings you the food.

  • The Kitchen (Model): Well, the kitchen would then be the model. This is what prepares the food, it manages the ingredients—your data, I guess, in an application.

  • The Presentation (View): And then there's the presentation—how the food looks on the plate when it arrives. It's your view.

What this looks like in Laravel terms:

  • Controllers handle your requests and coordinate responses

  • Models manage your data and your business logic

  • Views actually display the information to your users

Now, we've already seen what a view looks like. Each one has its own job, but your controller and your model haven't necessarily been shown to you in this course just yet. So let's go ahead and create your first controller!

Step 1: Creating Your First Controller

Just for simplicity's sake, why don't we move everything that we have in this routes file—Route::get and then return view of home—why don't we move this logic to a controller? That way you have a little bit more insight as to how this all fits within a Laravel application.

Right now in routes/web.php, you have:

1Route::get('/', function () {
2 return view('home');
3});

We're going to generate a controller. Now we can do this in our terminal for our application, or we can actually just do it in the terminal built into your code editor if there is one. I'm going to do that here just for simplicity's sake:

1php artisan make:controller ChirpController

Now I'm going to tell you what this command is, and then we're going to take a look at why this command actually exists. We're saying php artisan make:controller. But what is this php artisan? That is Laravel's way of running specific commands. It's Laravel's command line tool. It's your Swiss Army knife, I guess, for Laravel development.

We use it a lot to create things—not just controllers, but there are a ton of artisan commands that are available to be able to run anything within Laravel. And the cool thing is you can actually create your own as well! For right now, we're just going to be using a lot in this "make" directory of php artisan commands, mostly to make controllers, and later we'll make models, maybe database migrations as well. Artisan commands are incredibly powerful, so there's so much to dive into.

If we press enter (and if you didn't add the controller name), it's automatically going to prompt for what that controller should be named. You can even see what that looks like typically in terms of casing and in terms of how it should be spelled. Because we are going to be creating chirps, we want a ChirpController.

We're going to stick with an empty controller for now, but later we'll go back and look at what "resource" actually provides for us.

And you can see it created a specific file for us: app/Http/Controllers/ChirpController.php. Let's go ahead and open up that new file:

1<?php
2 
3namespace App\Http\Controllers;
4 
5use Illuminate\Http\Request;
6 
7class ChirpController extends Controller
8{
9 //
10}

Step 2: Add an Index Method

It's just a blank file, but let's go ahead and add the things that we had in our web route here. How do we do that? We're going to add an index method to handle our homepage:

1<?php
2 
3namespace App\Http\Controllers;
4 
5use Illuminate\Http\Request;
6 
7class ChirpController extends Controller
8{
9 public function index()
10 {
11 return view('home');
12 }
13}

Don't forget the semicolon! And you can see here that if you're using the VS Code Laravel extension, it still takes us directly to that view that we have.

The index method is a convention for displaying a list of things (or in our case, the main page).

Step 3: Update Your Route

So in our web routes, how do we change this to instead point to our index method in our ChirpController?

Let's go ahead and clear this out. Instead of returning a view here, we're actually going to use the ChirpController. Update routes/web.php to use your controller:

1use App\Http\Controllers\ChirpController;
2 
3Route::get('/', [ChirpController::class, 'index']);

We're importing it in as a class, and then we can pass it a particular method. In this case, I'm going to be direct and pass it that index method.

Let's take a look at our browser... refresh, everything still works great! This is just a cleaner way of writing what we were doing directly in our routes file. But now all of this lives in our controller. Now we can slowly start to add to this controller as we build out our application.

Step 4: Passing Data to Views

Now controllers really shine when you need to prepare data and then serve that data. Again, similar to how a waiter might do this in a restaurant. We're going to add some sample chirps to see this in action:

1public function index()
2{
3 $chirps = [
4 [
5 'author' => 'Jane Doe',
6 'message' => 'Just deployed my first Laravel app! 🚀',
7 'time' => '5 minutes ago'
8 ],
9 [
10 'author' => 'John Smith',
11 'message' => 'Laravel makes web development fun again!',
12 'time' => '1 hour ago'
13 ],
14 [
15 'author' => 'Alice Johnson',
16 'message' => 'Working on something cool with Chirper...',
17 'time' => '3 hours ago'
18 ]
19 ];
20 
21 return view('home', ['chirps' => $chirps]);
22}

Now, how do we pass this into this view? Our second parameter is going to be an array. We're saying that 'chirps' is going to be equal to the $chirps array variable.

Now, how do we display these chirps in our view? Again, if we're talking about the MVC model-view-controller structure, we have our controller and we're passing these chirps to our view. Now, how do we display them here?

Well, this is where that Blade templating comes into play. So our home.blade.php file is not just HTML, it is Blade, and we can add some templating structure to this. Update your home.blade.php to display these chirps:

1<x-layout>
2 <x-slot:title>
3 Welcome
4 </x-slot:title>
5 <div class="max-w-2xl mx-auto">
6 @foreach ($chirps as $chirp)
7 <div class="card bg-base-100 shadow mt-8">
8 <div class="card-body">
9 <div>
10 <div class="font-semibold">{{ $chirp['author'] }}</div>
11 <div class="mt-1">{{ $chirp['message'] }}</div>
12 <div class="text-sm text-gray-500 mt-2">{{ $chirp['time'] }}</div>
13 </div>
14 </div>
15 </div>
16 @endforeach
17 </div>
18</x-layout>

In this case, for every chirp we can say @foreach ($chirps as $chirp), and then every single instance of chirps that we have in that array that is being passed in from this ChirpController, we're going to iterate over whatever HTML that we're passing in here. Don't forget the @endforeach to say that this is the end of that foreach loop.

Now we have available the $chirps array. We have the chirp author, the chirp message, and the chirp time to coincide with what we're passing in from the ChirpController.

Let's see what this looks like in our browser... Alright! That looks pretty good. Now you can see how our controller is going to allow us to pass additional data to different views, and we can also have different methods.

Step 5: Resource Controllers

Let's take a look at what resource controllers offer. What I'm going to do is I'm actually going to copy all of this and delete this ChirpController, because when we used that initial method to create a controller, we just selected empty.

What happens if we then create a ChirpController that is a resource controller? This is what you might typically use in a standard MVC structure. This resource allows for an optional model that you might have. We don't have a Chirp model just yet, so I'm not going to select anything there.

1php artisan make:controller ChirpController --resource

Just a hint—anytime we use an artisan command, we don't necessarily need to leave it blank. We could write ChirpController and then even pass it additional options or parameters. Most artisan commands have this ability where I could pass it --resource and it would automatically complete what we just had completed for us.

Now, in that ChirpController, we have seven different methods:

  • index() - Displaying a listing of all resources. It's listing all of the chirps, this home page.

  • create() - Might be showing a form to create a new chirp. In our demo application, we're doing this on the same page, so we likely won't use this.

  • store() - Where we're creating and saving a new chirp

  • show() - To be able to display one single chirp

  • edit() - Displaying a form for editing a single chirp

  • update() - Would be to update that single chirp

  • destroy() - Deleting a chirp

We're going to be using most of, if not all of these, eventually throughout our application, but for now, we're just using the index method. So just update the index method with our sample data from before.

The MVC Flow

So to recap, here's what happens when someone visits your site:

  1. Route - This slash parameter, this root of your application, receives the request

  2. Route calls the ChirpController, specifically the index method of this ChirpController class

  3. Controller prepares the data—it's waiting at our table to decide how are we going to present this data (our sample chirps)

  4. Controller passes all this information to the view—in this case, that home.blade.php

  5. View then renders that HTML with the data

  6. User sees this page!

Why This Matters

So why does all this matter? Well, right now we're using fake data, but imagine when we have thousands of real chirps in a database:

  • The Controller that we just set up is going to decide which chirps to show. It's going to take our order and then decide how to present them.

  • The Model (which we're going to learn about in the next lesson) fetches them from the database.

  • The View (that home.blade.php, at least for our index) displays them nicely.

So each piece has its job in the MVC structure—Model, View, Controller. Your code stays organized, and then future you will thank current you for how things are organized and displayed to your user.

What's Next?

Our chirps are just arrays in memory right now. So in our next lesson, let's start working with real data to see how your app might remember things permanently. We'll set up a real database and create our first Model to manage actual data!

00:00
What is MVC?
00:01
Well, right now, your route in this web PHP routes file, it's doing everything.
00:08
It's handling the request, it's preparing data, it's returning a view, and it might
00:12
not seem like it's doing that mostly because Laravel does all that for you
00:16
in this route facade that's getting this slash directory and it's returning a view
00:22
and it's simple, but it's doing a lot.
00:25
Under the hood.
00:26
So what does this look like?
00:27
As your application grows, you'll want a better way to organize things, and
00:31
that's where the MVC model comes in.
00:34
So what is MVC?
00:35
MVC stands for?
00:37
Model view controller.
00:39
And if we're gonna take this into an analogy, let's think of a restaurant
00:43
while MVC is not necessarily the order that everything happens in
00:47
terms of model and then view and then controller within Laravel, let's.
00:52
Talk about the controller first.
00:53
This is what you would think of as the waiter in a restaurant.
00:57
It takes your order, it coordinates everything, and
00:59
then it brings you the food.
01:02
Well, the kitchen would then be the model.
01:04
This is what prepares the food, it manages the ingredients, the, your
01:08
data, I guess, in an application.
01:10
And then there's the presentation, how the food looks on the plate.
01:14
When it arrives.
01:15
It's, it's your view.
01:17
What this looks like in Laravel terms, the controllers handle your requests.
01:21
It coordinates responses while your models manage your data and your business
01:26
logic, and then your views actually display the information to your users.
01:30
Now, we've already seen what a view looks like.
01:32
Each one has its own job, but your controller and your model
01:36
haven't necessarily been shown to you in this course just yet.
01:40
So let's go ahead and create your first controller.
01:42
That way you have a little bit more insight as to how this all
01:45
fits within Alar application.
01:47
Just for simplicity's sake, why don't we move everything that we
01:50
have in this routes file route gets, and then return view of home.
01:56
Why don't we move this logic to a controller?
01:59
We're gonna generate one now we can do this in our terminal for
02:02
application, or we can actually just do it in the terminal, built into
02:05
your code editor if there is one.
02:07
I'm gonna do that here just for simplicity's sake.
02:10
We could run PHP Artisan and then a command.
02:16
Now I am going to tell you what this command is, and then we're gonna take a
02:19
look at why this command actually exists.
02:23
We're gonna say PHP Artisan make.
02:26
Controller.
02:27
Then we can give a name of a controller here.
02:31
But what is this?
02:32
PHP Artisan that is lar v's.
02:35
Way of running specific commands.
02:38
It's Lev's command line tool.
02:40
It's your Swiss Army knife.
02:43
I guess for Laravel development.
02:45
We use it a lot to create things, not just controllers, but there are a ton
02:50
of artisan commands that are available.
02:53
To be able to run anything within Laravel, and the cool thing is you
02:58
can actually create your own as well.
03:01
For right now, we're just gonna be using a lot in this make directory of
03:05
PHP Artisan commands, mostly to make controllers, and later we'll make models
03:11
maybe database migrations as well.
03:14
Artisan commands are incredibly powerful, so there's so much to dive into.
03:19
But for now, let's go ahead and make a controller, PHP Artisan
03:24
Make Controller, and we don't have to add anything here afterwards.
03:28
If we press enter, it's automatically going to prompt for what that
03:32
controller should be named.
03:33
You can even see what that looks like typically
03:37
in terms of casing and in terms of how it should be spelled and maybe handled
03:42
with an layer of application because we are going to be creating chirps.
03:46
We want a chirp controller, so chirp controller.
03:51
We're gonna stick with an empty controller for now, but later we'll go back and look
03:56
at what resource actually provides for us.
03:59
And you can see it created a specific file for us.
04:02
This app, HTP controller's, chirp controller.
04:05
Alright, let's go ahead and open up that new file that I created.
04:08
Again, that's app, HTTP, controllers, chirp controller.
04:14
It's just a blank file, but let's go ahead, add the things that
04:18
we had in our web route here.
04:20
How do we do that?
04:21
We're gonna add an index method, that's public function index,
04:26
and then we're just gonna return a view.
04:28
We're gonna return a view of home.
04:30
Don't forget the semicolon.
04:32
And you can see here that our VS code, Laravel extension still takes
04:36
us directly to that view that we have.
04:39
So in our web routes,
04:40
how do we change this to instead point to our index method in our chirp controller?
04:46
Let's go ahead and clear this out\ so instead of returning a view here,
04:50
we're actually going to use the chirp controller or importing that in.
04:55
It's gonna be a class and then we can pass it a particular method.
04:58
In this case, I'm going to be direct and pass it that index method.
05:02
Let's take a look at our browser
05:04
refresh, everything still works great.
05:07
This is just a cleaner way of writing what we were doing directly in our routes file.
05:12
But now all of this lives in our controller.
05:15
Now we can slowly start to add to this controller as we
05:19
build out our application.
05:20
Now controllers really shine when you need to prepare data and then serve that data.
05:25
Again, similar to how a waiter might do this in a restaurant, we're gonna add
05:30
some sample chirps to see this in action.
05:33
So in our index method, I'm going to add a chirps array, and this
05:38
is just some sample chirps with a.
05:41
Time and author, and then a message.
05:43
Now, how do we pass this into this view?
05:45
Our second parameter is going to be an array.
05:48
We're gonna say that this is
05:50
chirps and that chirps is going to be equal to the chirps array variable.
05:57
Now, how do we display these chirps in our view?
06:00
Again, if we're talking about the MVC model, view controller structure,
06:04
we have our controller and we're passing these chirps to our view.
06:08
Now, how do we display them here?
06:11
Well, this is where that blade templating comes into play, so a home blade PHP file
06:18
is not just HTML, it is Blade, and we can add some templating structure to this.
06:23
In this case, for every chirp we can say, hmm, maybe for each chirps as
06:31
chirp, and then every single instance of chirps that we have in that array.
06:37
That is being passed in from this CHIRP controller.
06:41
We're going to iterate over whatever HTML that we're passing in here.
06:46
I'm just gonna say end for each here to say that this is
06:50
the end of that for each loop.
06:52
Now we have available the chirps array.
06:55
We have the chirp author, the chirp message, and the chirp time.
07:00
I am going to paste that in here.
07:02
Again, the chirp.
07:04
That we're looping through the author, the message, and the
07:08
time to coincide with what we're passing in to the trip controller.
07:12
Let's see what this looks like in our browser.
07:14
All right.
07:14
That looks pretty good.
07:15
Now you can see how our controller is going to allow us to pass additional
07:19
data to different views, and we can also have different methods.
07:24
Let's take a look at what that looks like.
07:27
What I'm going to do is I'm actually going to copy all of this and delete
07:31
this chirp controller, because when we used that initial method to create
07:36
a controller, we just selected empty.
07:38
What happens if we then create a chirp?
07:40
Controller that is a resource controller.
07:44
This is what you might typically use in a standard MVC structure
07:49
model, view controller.
07:50
This resource allows for an optional model that you might have.
07:55
We don't have a chirp model just yet, so I'm not going to select anything there.
08:00
Now, in that chirp controller, we have seven different methods.
08:05
The index, create store, show, edit, and update and destroy.
08:11
These are all methods that you would probably typically use within
08:15
any standard MBC application.
08:18
Just the hint.
08:18
Anytime we use an artisan command, we don't necessarily need to leave it blank.
08:23
We could write chirp controller and then even pass it additional,
08:28
uh, options or parameters.
08:30
Most artisan commands have this ability where I could pass it as the resource
08:35
and it would automatically complete what we just had completed for us.
08:40
Now you might start to see how each one of these seven different methods that
08:43
we have within a resource controller can be used within our application.
08:47
For example, the index, it's displaying a listing of all resources.
08:51
It's listing all of the chirps, this home page.
08:55
The create method might be showing a form to create a new
08:59
chirp in our demo application.
09:02
We're doing this on the same page, so we likely won't use this.
09:05
The store where we're creating and saving a new chirp the public function show
09:10
to be able to display one single chirp edit, displaying a form for editing.
09:17
A single chirp update would be to update that single chirp.
09:22
Then destroy, deleting a chirp.
09:25
We're going to be using most of, if not all of these, eventually
09:30
throughout our application, but for now, we're just using the index method.
09:33
So to recap, here's what happens when someone visits your site.
09:37
This route, this slash parameter, this root of your application is then
09:43
going to pass the chirp controller, specifically the index method
09:47
of this chirp controller class.
09:50
The controller prepares the data, it's waiting our table to decide how
09:54
are we going to present this data?
09:56
And the controller passes all this information to the view, in this case,
10:01
that home blade, not PHP, where that view then renders that HTML with the
10:06
data and then the user sees this page.
10:10
So why does all this matter?
10:11
Well, right now we're using fake data, but imagine when we have thousands
10:14
of real chirps in a database, the controller that we just set up.
10:17
It's going to decide which chirps to show.
10:20
It's going to take our order and then decide how.
10:24
To present them the model, which we're gonna learn about in the next lesson,
10:27
it fetches them from the database.
10:29
Then the view that home blade php, at least for our index, displays them nicely.
10:35
So each piece has its job and the MVC structure model, view controller,
10:39
your code stays organized, and then future you will, will thank
10:43
current you for how things are organized and displayed to your user.
10:48
I. So in our next lesson, let's start working with real data to see how your
10:52
app might remember things permanently.