DEV Community

Cover image for MVC and creating it in Laravel 8
Graham Morby
Graham Morby

Posted on

MVC and creating it in Laravel 8

So let's assume you are brand spanking new to laravel and MVC frameworks, that you have never created a back-end system before or you are simply looking for new ways to do some things.

So what is MVC?

MVC (Model–view–controller) is a design pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements. This is done to separate internal representations of information from the ways information is presented to and accepted by the user.

The view itself is an easy one to understand, it's simply that, the view to which the user interacts.

The model is the central component of the pattern. It is the application's dynamic data structure, independent of the user interface. It directly manages the data, logic, and rules of the application.

The controller accepts commands from the model and converts them so the data can be used by the view.

Alt Text

Ok so that's a basic description of MVC and now let's have a look at what Laravel does and how we can use the above pattern to maximum effect.

So we have a brand new laravel install and we are sat with our code editor, ready to start. First and foremost we want to create a model, lets say we are building a basic article system. So to create the model we need to open a terminal window and type the following.

php artisan make:model Articles -m
Enter fullscreen mode Exit fullscreen mode

Ok, so what's the above doing? Well firstly in our files we have a folder called 'app'. The above command will create a brand new model in that folder called, You guessed it! Articles. This is the first part of our MVC pattern. But we are doing something else as well? what does the '-m' mean. Well, what that does is create database migration. So what does a Database migration do? Well, the Laravel website tells us this "Migrations are like version control for your database, allowing your team to define and share the application's database schema definition." and if you want more information check this link

So we have a migration that is attached to the model! Perfect! So all we need now is a controller. Now, this tutorial isn't about actual code and logic but about getting a base set up and understanding what it is we are doing. So if we create a controller we can attach the model and use it.

php artisan make:controller Articles --resource 
Enter fullscreen mode Exit fullscreen mode

If we now head to app/http/controllers we can see we have a controller with some functions already in place. These functions allow us to connect routes in a second. But basically, this allows us to use Restful patterns within our apps. The biggest benefit here is coding time with all this done for us and also we are writing less code with a neat and tidy codebase.

To learn more about how resources work look at

If we now go to routes/web.php and add the following code

use App\Http\Controllers\ArticlesController;

Route::resource('articles', ArticlesController::class);
Enter fullscreen mode Exit fullscreen mode

We would now have all routes that match our controller functions, so if you run

php artisan route:list
Enter fullscreen mode Exit fullscreen mode

You will see them all in your list! Perfect and ready to code! But we forgot one thing! We need to match our Model to our controller. So if you head over to our controller and at the top add

namespace App\Models\Articles;
Enter fullscreen mode Exit fullscreen mode

That will tell the controller what Model to use and now we have a perfect circle and everyone knows exactly what we are doing, but most of all we are ready to code.

Top comments (0)