Understanding Laravel MVC
Tags: laravel, mvc
Published: 2022-06-17 06:12:23

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:

```terminal
$ php artisan make:model User -m -f
```
Result:



![result make model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuuccnsopode8wmhm34q.png)


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](https://laravel.com/docs/9.x/migrations) a [factory](https://laravel.com/docs/9.x/database-testing) file and controller files. See an example below:

```terminal
$ php artisan make:controller UserController
```
Result:



![result make usercontroller](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kbgr0zqmme5h9jg8cvd.png)

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


### Model

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](https://laravel.com/docs/9.x/eloquent-collections). A model can look like this:

```php
  #attributes: array:10 [▼
    "id" => 51
    "name" => "Colin Renkema"
    "email" => "colin.sk.renkema@gmail.com"
    "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"
  ]
```
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.

### VIEW

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

```php
    @include('layouts.header')
    @yield('body')
    @include('layouts.footer')
```
Example result:



![layout example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/stkm5564x8jk6o3sbl54.png)

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:

```php
  <div class="">
     <p class="font-semibold">Personal information:</p> 
     Name:
     {{ Auth::user()->name }} // authenticated user model 
     <br>
     Email:
     {{ Auth::user()->email}} // authenticated user model 
     <br>
     Member Since:
     {{ Auth::user()->created_at }} // authenticated user model 
  </div
```
 As a result:


![Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nf7iskb89b5pmxv1xlpz.png)


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

### Controller

The [controller](https://laravel.com/docs/9.x/controllers) is where the logic happens, where we can process the data. For example:

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

```
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](https://laravel.com/docs/9.x/eloquent-relationships), 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.

### Route

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:

```php
Route::get('/profile', [ProfileController::class, 'userInfo']
)->middleware(['auth'])->name('profile');
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6yzxamr5kjcy6mkxm9hy.png)