Working With Models in Laravel 5.2 Image Working With Models in Laravel 5.2

Table Of Contents

This tutorial is part of the Building a blog with Laravel 5.2 course.

Working with Models

In a traditional MVC architecture, the model is where all the data, rules and logic for an application are written. The same concept applies to Laravel 5.2 applications.

In this tutorial I’m going to be showing you how we can create and work with some basic models in our new blog.

Why Do We Need Models?

Model’s are incredibly powerful in Laravel as they allow us to do things like define relationships. Say for instance you had a post on your blog and you wanted to link comments to that post, in Laravel it’s incredibly easy to define this relationship within a Model. You’ll see how we can define this Post -> Comment relationship further into the tutorial series.

Creating a Model

Again we are going to be utilizing php artisan in order to create a new model. For the blog we are building, we want to create a Post model that will interact with our posts database.

Now this is where Laravel shows it’s power, by simply creating a posts database table and then a Post model, Laravel will behind the scenes link these two things together without any added configuration.

Navigate to the root directory of your project and type the following in the console:

php artisan make:model Post

This should create a new Post.php class under your app directory.

Under Construction

This series is still undergoing construction. Stay tuned for more updates.

Was This Post Helpful?
Submit a PR: Edit on Github