Advertise Here

Eloquent ORM (Object Relational Mapping) in Laravel

  Suresh Ramani   Laravel   4 months ago
Eloquent ORM (Object Relational Mapping) in Laravel - TechvBlogs

 Laravel is a free, open-source PHP framework, created by Taylor Otwell. It follows a Model-View-Controller (MVC) design pattern. Laravel reuses the existing components of different frameworks which helps to create a perfect and outstanding web application. If you are familiar with PHP, Advance PHP it will make your task easier. Laravel is secure and prevents web attacks using their CSRF (Cross-site Request Forgery) protection.

Eloquent ORM is very useful for web development. It's very helpful for database operations like Create, Update, Delete, and any other operations. With Eloquent ORM you don't need to execute a query like PHP for create, update, delete, etc. Also, With Eloquent ORM we can fetch relational data with an easy method, Just we need to add some functions, and then It fetches records from a database like Pro. In this blog, we will cover some topics about Eloquent ORM.

First We Need to create a model with this simple Command:

php artisan make:model Category

This Command will create the Model file. If you are using Laravel 8+ then you can find this file on this location app\Models\Category.php, If you are not using Laravel 8+ then this file will create on this location app\Category.php

Here example of category model as we created first:

<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model{
   use HasFactory;

   //
}

?>

You can create a database migration file and seed some dummy data to the database using Faker. After finishing these things you just need to run this command to insert dummy data to the database:

php artisan db:seed --class=<Class name of seeder>

Crud with Eloquent ORM

Eloquent ORM makes it incredibly easy to perform CRUD ( Create, Read, Update, Delete ) operations on a Database using Laravel Model.

Create Record

To insert a record in the database table just need to create an instance of the model and set or assign attributes value and then call save() method:

$category = new Category;
$category->name = "Fruits";
$category->slug = "fruits";
$category->save();

Read Record

In Laravel Eloquent Model, If you want to retrieve all records then simply call all() method:

$categories = Category::all();

If you want to retrieve some conditional data then use where()  method:

$categories = Category::where('id',1)->get();

Update Record

To Update Record you retrieve that record and set the value to attributes and then call save() method:

$category = Category::find(1);
$category->name = "Fruit";
$category->save();

Delete Record

For Delete Record just find record and call delete() method:

$category = Category::find(1);
$category->delete();

As you can see here is a simple blog about the Laravel Eloquent Model in the upcoming blog we will deep dive into the topic of the Laravel Eloquent Model.

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

 

Suresh Ramani - Author - TechvBlogs
Suresh Ramani
Laravel & Vue.js Developer
Advertise Here