How To Use A Laravel Model – Eloquent ORM

A Laravel Model makes it very easy to store, read (retrieve), update and delete (CRUD) a resource in a Laravel application. In this post, I am going to show you how to use a laravel model to manage blog posts. I touched on this in my previous post when I talked about using laravel resource controllers here. The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding “Model” which is used to interact with that table.

how to use laravel model

In my post on laravel resource controllers, I mentioned that you use resource controllers to manage resources. Often, the resources would be stored in a database. That involves using tables to insert, update, query, delete and other operations that make up CRUD. Using our Post as an example (a blog post like the one you are reading right now), we will learn how to create a model for it.

How To Use a Laravel Model – Example Code

Our Post model will extend Eloquent (that makes it an Eloquent model) – which provides most of the methods we will need to work with resources.


I have explicitly told Eloquent which table to use above even though I didn’t have to. Eloquent will use the lowercase plural version of the class name; in this case – posts for the table name. Awesome right? That is all you need to have a working model in laravel.

Soft Delete In Laravel Model

One great feature that I like is Soft Deletes.

Soft deletes enable you to delete a blog post without actually deleting it from your table; instead, it adds a deleted_at column. Here is how you can add that functionality to your model.

Mass Assignment in Laravel Models

When you create a Post, you could pass an array of attributes – title, body, author, tags into the constructor of your laravel model. They will then be inserted into the table through mass assignment. This can be a problem security-wise. Why? Someone could insert anything they want into your database – including sql commands. To avoid such a security risk, Eloquent lets you set a protected attribute called $fillable; here is how we do it in our Post laravel model.

There are more attributes you could set for your model, but for now, let us move on to the next step.

Using Laravel Model Create Method

Once you have defined your laravel model, you can create an instance and set the attributes then save it to the database; creating tables is not necessarily covered in this post – will do so in my next post. It is very easy using migrations. Here is how we use the laravel Model Create method to create new resources in your controller.


Fetching Posts From the Database

After storing your blog posts in a table, you can easily fetch and pass them to the view. Here is how you do that in your controller:

Restore My Soul

If you accidentally deleted a post, you can easily restore it by using this simple method;

How awesome is that huh?

I feel like I should stop here and let you digest what we have talked about so far. Next post will show you how to use laravel migrations to create tables – pretty easy stuff. I hope you will join me as I do that probably tomorrow. You can learn more about laravel models here.

If you found this post helpful to you, please consider sharing with your friends using the buttons below and dropping me a line through the comments or even contact me. Thank you for stopping by and keep being awesome!

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

One Comment on “How To Use A Laravel Model – Eloquent ORM

  1. talal Reply

    October 26, 2017 at 11:31

    how i can select blob column in laravel

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.