Slide 1

Slide 1 text

LARAVEL LIVE INDIA 1 LEARN | CONTRIBUTE | EXPAND | SHARE

Slide 2

Slide 2 text

LARAVEL LIVE INDIA 2 Laravel Eloquent & Its Hidden Power

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

LARAVEL LIVE INDIA 4 While Working With Framework One Should Leverage Its Power.

Slide 5

Slide 5 text

LARAVEL LIVE INDIA 5 Let’s See Hidden Powers of Eloquent

Slide 6

Slide 6 text

LARAVEL LIVE INDIA 6 Clone a Model • Trick to make a clone of database row. • Use the replicate method of Eloquent

Slide 7

Slide 7 text

LARAVEL LIVE INDIA 7 DEMO

Slide 8

Slide 8 text

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.

Slide 9

Slide 9 text

LARAVEL LIVE INDIA 9 DEMO

Slide 10

Slide 10 text

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.

Slide 11

Slide 11 text

LARAVEL LIVE INDIA 11 DEMO

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

LARAVEL LIVE INDIA 13 DEMO

Slide 14

Slide 14 text

LARAVEL LIVE INDIA 14 Get Changed Values • Trick to get all of model attributes whose values are changed. • Use the getChanges method of eloquent.

Slide 15

Slide 15 text

LARAVEL LIVE INDIA 15 DEMO

Slide 16

Slide 16 text

LARAVEL LIVE INDIA 16 QUIZ

Slide 17

Slide 17 text

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()

Slide 18

Slide 18 text

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()

Slide 19

Slide 19 text

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.

Slide 20

Slide 20 text

LARAVEL LIVE INDIA 2 0 DEMO

Slide 21

Slide 21 text

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.

Slide 22

Slide 22 text

LARAVEL LIVE INDIA 2 2 DEMO

Slide 23

Slide 23 text

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.

Slide 24

Slide 24 text

LARAVEL LIVE INDIA 2 4 DEMO

Slide 25

Slide 25 text

LARAVEL LIVE INDIA 2 5 Model Keys • Can be used fetch specific attribute or column. • Use method modelKeys provides by eloquent.

Slide 26

Slide 26 text

LARAVEL LIVE INDIA 2 6 DEMO

Slide 27

Slide 27 text

LARAVEL LIVE INDIA 2 7 QUIZ

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

LARAVEL LIVE INDIA 3 0 New Query • Can be used to add global WHERE condition. • Override new query in model and specify condition there.

Slide 31

Slide 31 text

LARAVEL LIVE INDIA 31 DEMO

Slide 32

Slide 32 text

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.

Slide 33

Slide 33 text

LARAVEL LIVE INDIA 3 3 DEMO

Slide 34

Slide 34 text

LARAVEL LIVE INDIA 3 4 Eloquent Observers • Kind of an event listeners for Model • Can be used with CRUD operations.

Slide 35

Slide 35 text

LARAVEL LIVE INDIA 3 5 DEMO

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

LARAVEL LIVE INDIA 3 7 Thank You