Download - Eloquent ORM
![Page 1: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/1.jpg)
Eloquent
![Page 2: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/2.jpg)
Laravel
b j e c t
e l a t i o n a l
a p p e r
O
R
M
M o d e l V i e w C o n t r o l l e r
![Page 3: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/3.jpg)
– Wikipedia
«(…) a programming technique for converting data between incompatible type systems in object-oriented programming languages.»
![Page 4: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/4.jpg)
– Fredrik
«Salvation»
![Page 5: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/5.jpg)
Biblioteksempel
Verdal Bibliotek
Beatles
Maskeblomstfamilien
![Page 6: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/6.jpg)
Kråkefot
bookslibrariesVerdal Bibliotek Beatles
![Page 7: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/7.jpg)
Tabeller
booksid title year library_id
1 Beatles 1984 1
2 Maskeblomstfamilien 2003 1
3 Gutten som ville være en av gutta 1992 2
4 Amatøren 1977 2
librariesid name
1 Verdal Bibliotek
2 Levanger Bibliotek
libraries books
![Page 8: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/8.jpg)
SQL
booksid title year library_id
1 Beatles 1984 1
2 Maskeblomstfamilien 2003 1
3 Gutten som ville være en av gutta 1992 2
4 Amatøren 1977 2
librariesid name
1 Verdal Bibliotek
2 Levanger Bibliotek
SELECT * FROM books WHERE library_id = 1
![Page 9: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/9.jpg)
SQL
booksid title year library_id
1 Beatles 1984 1
2 Maskeblomstfamilien 2003 1
3 Gutten som ville være en av gutta 1992 2
4 Amatøren 1977 2
librariesid name
1 Verdal Bibliotek
2 Levanger Bibliotek
SELECT * FROM libraries l, books bWHERE l.id = b.library_idAND l.id = 2
![Page 10: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/10.jpg)
SQL
booksid title year library_id
1 Beatles 1984 1
2 Maskeblomstfamilien 2003 1
3 Gutten som ville være en av gutta 1992 2
4 Amatøren 1977 2
librariesid name
1 Verdal Bibliotek
2 Levanger Bibliotek
SELECT l.name, COUNT(*) numOfBooks FROM books bINNER JOIN libraries lON b.library_id = l.idWHERE b.id IN ( SELECT id FROM books WHERE year > 1980)GROUP BY l.idORDER BY name
name numOfBooks
Levanger Bibliotek 1
Verdal Bibliotek 2
![Page 11: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/11.jpg)
To Entiteter
bookslibraries
![Page 12: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/12.jpg)
![Page 13: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/13.jpg)
Titteltekst
• Masse SQL å holde styr på
• Krevende å skrive spørringer
• Utfordrende å drifte
• Potensiale for SQL-injection
Problemet
![Page 14: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/14.jpg)
SQL-injection
mysql_query('INSERT INTO students (name) VALUES (' . $name . ')');
INSERT INTO students (name) VALUES ('Robert'); DROP TABLE students;--');
Robert'); DROP TABLE students;--
![Page 15: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/15.jpg)
xkcdExploits of a Mom
![Page 16: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/16.jpg)
Putting the M in MVC
class Library extends Model { public function () { return $this->hasMany( ::class); }}
books
Book
L i b r a r y
![Page 17: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/17.jpg)
Putting the M in MVC
class Book extends Model{ public function () { return $this->belongsTo( ::class); }}
library
Library
B o o k
![Page 18: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/18.jpg)
Find
Book::find(1);
SELECT * FROM books WHERE id = 1;
S Q L
E l o q u e n t
![Page 19: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/19.jpg)
Where
Book::where(‘library_id’, 1)->get();
Library::find(1)->books;
SELECT * FROM books WHERE library_id = 1;
S Q L
E l o q u e n t
![Page 20: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/20.jpg)
Has & WhereBetween
Book::has('library')->whereBetween('year', [1980, 2000])->get();
SELECT * FROM `books` WHERE EXISTS ( SELECT * FROM `libraries` WHERE `libraries`.`id` = `books`.`library_id`) AND `year` BETWEEN 1980 AND 2000;
S Q L
E l o q u e n t
![Page 21: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/21.jpg)
Spørring på Relasjoner
Library::find(2)->books()->where('year', '<', 2000)->get();
SELECT * FROM `books` WHERE `books`.`library_id` = 2AND `books`.`library_id` IS not null AND `year` < 2000;
S Q L
E l o q u e n t
![Page 22: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/22.jpg)
Method Chaining
Part::find(1)->car->factory->manufacturer->manager->address;
![Page 23: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/23.jpg)
Create & Update
$book = Book::find(1);
$book->year = 1985;
$book->save();
$book = new Book;
$book->title = 'Halvbroren';$book->year = 2001;
$book->save();
C r e a t e U p d a t e
![Page 24: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/24.jpg)
Destroy
$book = Book::find(1);
$book->destroy();
![Page 25: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/25.jpg)
Titteltekst
• Eliminerer i stor grad SQL
• Portabilitet
• Objektorientert Query-språk
• SQL-injection usannsynlig
• Holder oss tørr (dry)
Fordeler
![Page 26: Eloquent ORM](https://reader035.vdocuments.mx/reader035/viewer/2022063020/58cef5951a28abab738b4e0f/html5/thumbnails/26.jpg)
Takk!