Updated April 17, 2023
Introduction to Laravel ORM
In Laravel we have ORM which basically stands for Objects relation model and Laravel include Eloquent, which helps us to interact with the database. Here ORM is basically related to the database side, where it helps us to interact with it, by that, we can perform delete, insert, update and other operations what is generally do it helps us to map our database entity with the corresponding code model. Every table in the database associates with the model, in order to perform an action on them after fetching it from the database. So in Laravel, it provides us Eloquent ORM framework which helps us to perform the database operations easily.
How does Laravel ORM work?
Laravel provides us which is Eloquent helps us to interact with the database, and allow us to perform the database operations. ORM is generally an object relation mapper which simply means that whatever the objects or model we have in our code maps them to the database entities or table. So for this Laravel provide us with Eloquent which helps us to map our models from code to database tables and allows us to perform the operations as well. Here we will see the implementation of the ORM model using Eloquent in Laravel.
In order to represent the model using table we can follow the below syntax for this:
class Demo extends Eloquent {
public static $table = 'demo';
}
Here in this above syntax, we will be going to refer to our ‘demo’ table with the name given below not with one the convection.
By the use of ORM, we can perform different types of operations such as, insert, sort, delete, update and any more.
- First, we have to create the model inside our code. Suppose you are maintaining student records inside our database so you might have tables related to that.
- So to represent the student models we have to create the ORM for this.
- We can create the student ORM using the Eloquent framework or library provided by the Laravel.
- Once we are done with the ORM creation for students we can use them against the table in the database.
- This ORM will contain the columns which is going to represent the columns inside the table.
- We can have the explicit name of the table defined rather than using the convection one.
- Once we are done with the creation of the student ORM we can now interact with the database using Eloquent.
- So for us, it will convert the student model with the table one, and map all the columns for us.
- In this way to helps to interact with the database, in other languages, we have different kinds of ORM available which perform the same thing.
Create Laravel ORM
Here we will see the creation for ORM in Laravel using the Eloquent library because in every framework or language we always have one framework which will handle this ORM part for us, we cannot do this without the formwork.
The creation part of ORM in language.
1. create
Follow this below syntax to create the ORM in language using Eloquent.
class NAME extends Eloquent {
public static $table = 'TABLE_NAME';
}
As you can see in the above syntax we are trying to create the ORM using Eloquent, for this, we have to extend the ‘Eloquent’ after this standard syntax we need to follow in order to create it properly.
- First, we can specify the class name.
- After the class name, we can extend the ‘Eloquent’.
- To specify the name of our table we can use ‘$table’ followed by the name of the table.
After edit the syntax will look like below:
class Student extends Eloquent {
public static $table = 'student';
}
This is the basic step to create the initial structure of the ORM. Now in order to automatically manage the timestamp part Laravel provides us a timestamp which will automatically update the timestamp inside the column.
class Student extends Eloquent {
public static $timestamps = true;
}
This will automatically update the create_at/ update_at columns in the table. Now we can see few methods which can help us to retrieve the record from the database.
1. To find students with id: For this, we can use find() method, below is the syntax to use this.
$student = Student::find($student_id);
This will give us the result in the ORM that we have created above. Just mentioned the name of your class with the id parameter here to find the record inside the database table.
2. Retrieve all the records from the database: We can retrieve all the records from the database using the below command.
$student = Student::all();
3. Also we can make use of a query aggregator which helps us to retrieve the custom query logic see below to count.
$count = Employee::where('Salary', '>', 200)->count();
Fish Model
Fish is a library which provides us with different types of packages to handle loggers, validation, tabs and many more.
- fish/eloquent-logger: This package from fish allows us to logs all the operation into the destined table which include (add, delete, update). To add this inside the application below dependency is required inside the composer.json file.
“fish/eloquent-logger”: “^1.0”
- fish/one-validator: This package help invalidation, it will convert the server-side validation to a specific jQuery validate plugin, to install this add the below dependency inside the composer.json file.
“fish/one-validator”: “dev-master”
Using the Following Artisan Command
It is a CLI, which stands for a command-line interface which is included with Laravel. It provides us list of commands to build our application few of them are mentioned below.
1. For the help window execute the below command.
php artisan help migrate
2. List of artisan command: To view all the commands provided by artisan execute the below command.
php artisan list
Conclusion
By this, we understood how to interact with database tables and convert them to code models or simple in POJO classes. This helps us to map our object to a table database table. To simplify this language provide us with Eloquent library which will be easy to use and implement by the developers, readable and understandable as well.
Recommended Articles
We hope that this EDUCBA information on “Laravel ORM” was beneficial to you. You can view EDUCBA’s recommended articles for more information.