Laravel 5 Eloquent Explained in the Simplest Way -Part 1

Rakshith Vasudev
3 min readJul 16, 2016

--

Let me get one thing clear: This article is for beginners who stumble upon every video and post with a hope to learn what Eloquent actually means in the Laravel Eco System. If you’re coming from a different POV other than mine, you’re welcome to comment. But, this is just the way how I got introduced to Laravel & I believe that if I can help people by telling what I know now, I can save a lot of time for them.

Ok, Let’s get started.

In Simple terms, eloquent is a way by which you can convert your database stuff such as, accessing the records or columns into a class and treat them like regular variables. ActiveRecord implementation is made possible because of Eloquent.

Things such as adding, deleting, updating to database doesn’t have to be written in the form of big queries because of Eloquent.

How do you begin using Eloquent?

Simple : by creating a Model. In my case, I have a table called articles. So In order to create a model for that table there are some naming conventions that need to be followed.

Here they are:

  1. Always have the first letter starting in the Block letter.
  2. Always convert plural to singular.

Here are 3 examples :

1 ) Table name : “articles” — Model name: “Article”.

2) Table name: “schools” — Model name: “School”.

2) Table name: “companies” — Model name: “Company”.

Yes, Laravel is an intelligent English Guy.

To create a Model, go to your console and type :

php artisan make:model Article

It’d say “model created successfully”. The model is created in the app directory.

If you open up the model “article.php” you’ll find this.

<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Article extends Model
{
}

What this does?

It creates a class called Article from the “articles” table.

So what if a class is created?

You can use every column from the table as an attribute like so:

$article=new Article;
$article->name = $request->name;
$article->content=$request->content;
$article->save();

For a better understanding let me show you the articles table.

So here, the article variable of the “Article” class has the following attributes because of the eloquent.

id, name, content, created_at, updated_at, categories.

So, you may access like so:

$articles->categories=”Some category” ;

In this article, I’ve just given an introduction to what do you mean by Laravel’s Eloquent, in the upcoming article, I’ll talk about Eloquent’s queries and Active Record implementation.

Here is a video where I’ve explained a little bit more.

Thanks for reading! :) If you liked it, hit that heart button below. Would mean a lot to me and it helps other people see the story.

Follow me on twitter: http://twitter.com/rakshithvasudev

Add me on snapchat : raksh123

Follow me on Instagram : https://www.instagram.com/rakshith.vasudev/

--

--