In this tutorial, we will discuss what is laravel eloquent model and why we use laravel eloquent orm model concepts instead of the query builder.
laravel eloquent model basics.
- What is laravel eloquent
- All commands to create model
- Eloquent model structure
- Laravel eloquent vs query builder
- Laravel eloquent basic topics
What is laravel eloquent model
Laravel eloquent is an object-relational mapper so it is also called eloquent orm model it is used to interact with databases and perform insert, update and delete operations. In simply it interacts activeRecords from the database. Model is a layer between database and controller
Models are listed in App/Model directory and you also change this by customizing in composer.json file but this is not the right way laravel provides default Model directory that are good and secure.
Laravel all commands to create model
The basic command to create a model is php artisan make:model Student
We also make model and database table migration by one command php artisan make:model Student -m
Or
php artisan make:model Student -migration
And if you create model, migration and controller by single command than use php artisan make:model Student -mc
Eloquent model structure
The below class Student is a basic eloquent model structure that are extends Model class.
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Student extends Model { // }
Laravel eloquent vs query builder
The basic difference between Laravel eloquent vs query builder is eloquent provides a large number of methods and relationships. Eloquent model data is form of collection that contain more information and fast.
The eloquent model provides relationships but it slow execution compare to query builder in simple queries but when query have more complex and data come from different relationships than eloquent model better because it provides a good relationship methods.
Laravel eloquent basic topics
In this series we will provide these basic eloquesnt concepts. and after this series also provide eloquent relationships concpts.
- basic model configurations
- insert data
- data reterive
- first vs find
- delete
- update
- update or create
- chunk and cursor method
- subqueryselect
- find or fail
- dat get and create
- aggregates
- isdirty , isclean waschanged
- laravel migration
Anmol Sharma
I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.
Related tutorial links
- What is laravel eloquent model and introduction
- part 1 laravel eloquent model all configuration hindi
- part 2 laravel insert and create data by eloquent model tutorial
- part 3 laravel data retrieve method
- part 4 first, find, findOrFail and firstOrFail method
- part 5 laravel delete vs truncate method
- part 6 laravel save vs update method
- part 7 laravel updateOrCreate and upsert
- part 8 laravel chunk vs cursor method
- part 9 laravel eloquent subquery select
- part 10 laravel findOrFail vs firstOrFail
- part 11 laravel create and retrieve data
- part 12 laravel eloquent aggregate functions
- part 13 laravel isDirty vs isClean vs wasChanged method