Author - StudySection Post Views - 115 views
orm

Introduction to ORM in Laravel

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

    1. 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 -m

      The model should look like the following:

      php1

      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
    2. Create Controller using a command like below
      Php artisan make: controller TestController

      ->Create a retrieve method to get data from the database.

      php2

      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 below

      4 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 table

      Collections

      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”}]

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri