Eloquent ORM (Object Relational Mapping) in Laravel
In this post we will give you information about Eloquent ORM (Object Relational Mapping) in Laravel. Hear we will give you detail about Eloquent ORM (Object Relational Mapping) in Laravel And how to use it also give you demo for it if it is necessary.
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 10+ then you can find this file on this location app Models Category.php, If you are not using Laravel 10+ then this file will create on this location app Category.php
Here example of the category model as we created first:
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
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 the 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.
Hope this code and post will helped you for implement Eloquent ORM (Object Relational Mapping) in Laravel. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs