Eloquent makes it very easy to interact with the application database with a new advanced technique for short queries using the model. ORM allows us to work with a database with short queries use objects and relationships between tables and it includes various methods to create, update and retrieve data from the database.
Eloquent ORM crud operations with example
-
To get data from the database from the database
-
We need to create a model using the command. Always use table name in the plural form and the model name is in the singular form. For example:- Model name is ‘Test’ and the table name is ‘Tests’.
Php artisan make:model Test
using below commands to create a migration
Php artisan make:model Test –migration
Php artisan make:model Test -mThe model should look like the following:
For Example Tests Table:-
id title hits created_at updated_at 1 test1 1 2019-11-28 12:17:32.8933333 2019-11-28 12:17:32.8933333 2 test2 0 2019-11-28 12:19:32.8933333 2019-11-28 12:19:32.8933333 3 test3 1 2019-11-28 12:24:32.8933333 2019-11-28 12:24:32.8933333 -
Create Controller using a command like below
Php artisan make: controller TestController->Create a retrieve method to get data from the database.
OUTPUT:
test1
test2
test3->Statement to get record from the database by the filter
$result = App\Test::where('hits', 1)
->orderBy('title', 'desc')
->take(2)
->get();
$jsonResult = $result->toJson();
echo jsonResult;OUTPUT:
[{“id”:”1,”title”:”test1″,”hits”:”1″,”created_at”:”2019-11-28 12:19:32.8933333″,”updated_at:”2019-11-28 12:19:32.8933333”},{“id”:”2,”title”:”test2″,”hits”:”0″,”created_at”:”2019-11-28 12:19:32.8933333″,”updated_at:”2019-11-28 12:19:32.8933333”}]->Create an insert method to insert data from the database.
public function insert()
{
$test = new Test();
$test->title = “test4”;
$test->hits= 4;
$test->save();
}OUTPUT:
The query will Insert new record as below4 test4 4 2019-11-28 12:24:32.8933333 2019-11-28 12:24:32.8933333 ->Create an update method to update record into the database
public function update()
{
$test = Test :: Find(1);
$test->title = “Test update 1”;
$test->hits= 2;
$test->save();
}OUTPUT:
id title hits created_at updated_at 1 Test update 1 2 2019-11-28 12:17:32.8933333 2019-11-28 12:17:32.8933333 ->Delete record from the table
$test = Test::find(1);
$test->delete();
OUTPUT:
It will delete the first row from the tableCollections
For eloquent methods for example ‘all’ and ‘get’, collections provide more functions to get the result in an easy way and we can also loop through collection results, for example,
contains, diff, except, find, fresh, load, etc. Below is the example of find
$result = Test::all();
$finalResult = $result->find(1);
$jsonResult = $finalResult->toJson();
echo $jsonResult;OUTPUT:
[{“id”:”1,”title”:”test1″,”hits”:”1″,”created_at”:”2019-11-28 12:19:32.8933333″,”updated_at:”2019-11-28 12:19:32.8933333”}]
-
We need to create a model using the command. Always use table name in the plural form and the model name is in the singular form. For example:- Model name is ‘Test’ and the table name is ‘Tests’.
Chunking Results
If we want to retrieve thousands of record we can use the chunk method. It will return some of the records with pagination. For example
Post::chunk(200, function ($result) {
foreach ($result as $value) {
//
}
});
OUTPUT:
It will return 200 records first time with pagination and you can navigate to other pages by pagination.
jQuery presents a tree-like structure of all the elements on a webpage simplifying the syntax and further manipulating such elements. The jQuery Certification exam by StudySection will secure your fundamental knowledge and a basic understanding of jQuery as an asset to improve your skills.