Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laravel Eloquent & Its Hidden Power

Hiren Dave
June 07, 2020
100

Laravel Eloquent & Its Hidden Power

What is Eloquent
Provides ActiveRecord implementation to interact with database.
Each Table has corresponding Model.
Use Model to do CRUD operations with database.
Let’s See Hidden Powers of Eloquent, like clone a model, get changed values, server to server copy etc.

Hiren Dave

June 07, 2020
Tweet

Transcript

  1. LARAVEL LIVE INDIA 3 What is Eloquent • Provides ActiveRecord

    implementation to interact with database. • Each Table has corresponding Model. • Use Model to do CRUD operations with database.
  2. LARAVEL LIVE INDIA 6 Clone a Model • Trick to

    make a clone of database row. • Use the replicate method of Eloquent
  3. LARAVEL LIVE INDIA 8 Increment or Decrement Values • Trick

    to increase or decrease values of Number columns. • Use the increment or decrement method of Eloquent.
  4. LARAVEL LIVE INDIA 10 Check if Record is Changed •

    Trick to determine if the model values are changed or not. • Use the isDirty method of eloquent.
  5. LARAVEL LIVE INDIA 12 Get Original Values • Trick to

    get old value of model attributes. • Use the getOriginal method of eloquent.
  6. LARAVEL LIVE INDIA 14 Get Changed Values • Trick to

    get all of model attributes whose values are changed. • Use the getChanges method of eloquent.
  7. LARAVEL LIVE INDIA 17 What is method of Eloquent to

    clone a row in table a) cloneRow() b) duplicateRow() c) replicate() d) copyRow() How to get old values of Model attributes using Eloquent? a) getInitialValue() b) getOriginalValue() c) getOldValue() d) getOriginal()
  8. LARAVEL LIVE INDIA 18 What is method of Eloquent to

    clone a row in table a) cloneRow() b) duplicateRow() c) replicate() d) copyRow() How to get old values of Model attributes using Eloquent? a) getInitialValue() b) getOriginalValue() c) getOldValue() d) getOriginal()
  9. LARAVEL LIVE INDIA 19 Accessors – Custom Values to Models

    • Allows you to create custom attribute to model. • This attributes / columns does not exist in table . • It’s created run time.
  10. LARAVEL LIVE INDIA 21 Mutators – For Non Existing Columns

    • Opposite of accessors. • Use non exiting column in model. • Use mutator to map it to correct column in database.
  11. LARAVEL LIVE INDIA 2 3 Eager Loading with $with •

    While working with relation ship get the data of related model immediately. • Set in model to always eager load related data. • Saves no of queries in database.
  12. LARAVEL LIVE INDIA 2 5 Model Keys • Can be

    used fetch specific attribute or column. • Use method modelKeys provides by eloquent.
  13. LARAVEL LIVE INDIA 2 8 What is use of Accessors

    in Eloquent a) To access row in table. b) Appending custom values to models. c) To get access of database. d) To get access of table. What keyword is used for Eager loading? a) $fetch b) $eager c) $prefetch d) $with
  14. LARAVEL LIVE INDIA 2 9 What is use of Accessors

    in Eloquent a) To access row in table. b) Appending custom values to models. c) To get access of database. d) To get access of table. What keyword is used for Eager loading? a) $fetch b) $eager c) $prefetch d) $with
  15. LARAVEL LIVE INDIA 3 0 New Query • Can be

    used to add global WHERE condition. • Override new query in model and specify condition there.
  16. LARAVEL LIVE INDIA 3 2 Server to Server or DB

    to DB Copy • Trick to have real time copy or record to different server or different database • Idea is to use connection property in Eloquent.
  17. LARAVEL LIVE INDIA 3 4 Eloquent Observers • Kind of

    an event listeners for Model • Can be used with CRUD operations.
  18. LARAVEL LIVE INDIA 3 6 Contact Me Website https://hiren.dev https://hirendave.tech

    Email [email protected] Social Media https://twitter.com/hjdave https://www.facebook.com/davehiren