DEV Community

Cover image for Understanding Laravel MVC
Colin Renkema
Colin Renkema

Posted on

Understanding Laravel MVC

In this post, I will share my knowledge of Laravel's MVC structure.

Model, View, Controller

MVC is a design pattern used in Laravel. It is a way to collect, deliver, and show data between computers. Laravel created a quick start for us, by providing some commands. In your terminal you can use for example:

$ php artisan make:model User -m -f
Enter fullscreen mode Exit fullscreen mode


result make model

Running this command will allow you to easily generate model files. MVC will also allow you to create model files, factory files and controller files. See an example below:

Running this command will make you a model file but also a migration a factory file and controller files. See an example below:

$ php artisan make:controller UserController
Enter fullscreen mode Exit fullscreen mode


result make usercontroller

As you can see, we have created a UserController file. So for what can we use does files?


Models are blue printed containers, to transport data from the database to the user and vice versa. By using a model you can get a data collection. A model can look like this:

  #attributes: array:10 [▼
    "id" => 51
    "name" => "Colin Renkema"
    "email" => ""
    "is_admin" => 1
    "classroom_id" => 2
    "email_verified_at" => "2022-06-10 12:10:56"
    "password" => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi"
    "remember_token" => null
    "created_at" => "2022-06-10 12:10:56"
    "updated_at" => "2022-06-10 12:10:56"
Enter fullscreen mode Exit fullscreen mode

This is the inside of a model with a segment of its data, which in this case is the user. We can send or collect this from or to the end user and show it in the view.


With view in Laravel we can use ".blade.php" files. We can create layouts by binding different blade files together, for an example:

Enter fullscreen mode Exit fullscreen mode

Example result:

layout example

In the ‘’index.blade’’ above, a layout is defined by including a header and footer blade file. The body will be different for every view it contains. An example of views we can display:

  <div class="">
     <p class="font-semibold">Personal information:</p> 
     {{ Auth::user()->name }} // authenticated user model 
     {{ Auth::user()->email}} // authenticated user model 
     Member Since:
     {{ Auth::user()->created_at }} // authenticated user model 
Enter fullscreen mode Exit fullscreen mode

As a result:


To identify the information we need to send or collect we need controllers.


The controller is where the logic happens, where we can process the data. For example:

 public function AddProduct($userId)
        $user = User::find($userId);
        $productId = $_POST['product'];
        return redirect(route('profile'))->with('message', 
        "Thank you! {$user->name} we will order this article 
        soon as possible.");

Enter fullscreen mode Exit fullscreen mode

The user submits a product and it will follow a route that can contain data to "public function addProduct". In this case the function is getting an "$userId". The model information is stored in "$user" like in model example, and the "$productId" contains the product number. Laravel knows the user and product model have a relation, so it can be combined in an database join table as a user that stores a product. To connect the MVC structure we have routes.


A route is like a railroad that carries the information to different station and also activates a function. A route can look like this for example:

Route::get('/profile', [ProfileController::class, 'userInfo']
Enter fullscreen mode Exit fullscreen mode

It will retrieve the command of the profile and trigger the function ‘’useInfo’’ in the ‘’ProfileController’’. If the user is authenticated it will continue to bring up the user information to the view. With the route in place I’m going to end this post. I hope the information was in any way useful to you. On to the next post!

Author said

Hello there! My name is Colin Renkema. Thank you for reading my first post. As a junior developer I’ve found sharing my knowledge is the perfect way to self-reflect, so please share any feedback you might have. Stay tuned for more!

csk logo

Top comments (0)