SlideShare a Scribd company logo
Object-Oriented
Programming
(with Laravel)
By: Shahrzad Peyman
Session 5
May-2017
shahrzad.peymaan@gmail.com
1
2
Today’s Presentation
• DataBase
• Configuration
• Multiple Connection
• Raw Queries
• Query Builder
• Pagination
3
DataBase
Laravel makes interacting with databases extremely simple
across a variety of database backends using either raw SQL,
the fluent query builder, and the Eloquent ORM. Currently,
Laravel supports four databases:
• MySQL
• Postgres
• SQLite
• SQL Server
Configuration
4
The database configuration for your application is located
at config/database.php.
In this file you may define all of your database
connections, as well as specify which connection should
be used by default.
Examples for most of the supported database systems
are provided in this file.
Using Multiple DataBase
Connection
5
When using multiple connections, you may
access each connection via the connection
method on th DB facade. The name passed to
the connection method should correspond to
one of the connections listed in your config/
database.php configuration file:
6
Running Raw SQL Queries
Once you have configured your database
connection, you may run queries using the DB
facade.
The DB facade provides methods for each type of
query: select, update, insert, delete, and
statement.
Running a Select Query
7
8
Running a Select Query
The first argument passed to the select method is
the raw SQL query, while the second argument is
any parameter bindings that need to be bound to
the query. Typically, these are the values of the
where clause constraints. The select method will
always return an array of results.
Running Insert/Update/Delete
Statements
9
Running a General
Statement
10
Some database statements do not return any
value. For these types of operations, you may
use the statement method on the DB facade:
DataBase Transactions
You may use the transaction method on the DB facade to
run a set of operations within a database transaction. If an
exception is thrown within the transaction Closure, the
transaction will automatically be rolled back. If the Closure
executes successfully, the transaction will automatically be
committed. You don't need to worry about manually rolling
back or committing while using the transaction method:
Manually Using Transaction
Query Builder
Laravel's database query builder provides a
convenient, fluent interface to creating and running
database queries.
It can be used to perform most database operations in
your application and works on all supported database
systems.
Retrieving All Rows From A Table
14
15
The get method returns an IlluminateSupportCollection
containing the results where each result is an instance
of the PHP StdClass object. You may access each
column's value by accessing the column as a property of
the object:
Retrieving All Rows From A Table
Retrieving A Single Row / Column From
A Table
16
Retrieving A List Of Column Values
17
Aggregates
18
The query builder also provides a variety of aggregate
methods such as count, max, min, avg, and sum. You may
call any of these methods after constructing your query:
19
Selects
20
Raw Expressions
Sometimes you may need to use a raw expression in a
query. These expressions will be injected into the
query as strings, so be careful not to create any SQL
injection points! To create a raw expression, you may
use the DB:raw method:
21
Inner Join
The query builder may also be used to write join statements.
To perform a basic "inner join", you may use the join
method on a query builder instance. The first argument
passed to the join method is the name of the table you need
to join to, while the remaining arguments specify the column
constraints for the join. Of course, as you can see, you can
join to multiple tables in a single query:
22
Left Join
If you would like to perform a "left join" instead of an
"inner join", use the leftJoin method. The leftJoin
method has the same signature as the join method:
Cross Join
23
To perform a "cross join" use the crossJoin method
with the name of the table you wish to cross join to.
Cross joins generate a cartesian product between
the first table and the joined table:
24
Advanced Join
24
25
Unions
The query builder also provides a quick way to
"union" two queries together. For example, you
may create an initial query and use the union
method to union it with a second query:
25
26
Where Clauses
You may use the where method
on a query builder instance to
add where clauses to the query.
The most basic call to where
requires three arguments. The
first argument is the name of the
column. The second argument is
an operator, which can be any of
the database's supported
operators. Finally, the third
argument is the value to
evaluate against the column.
27
Or Statements
28
Additional Where Clauses
29
Additional Where Clauses
30
Additional Where Clauses
31
Parameters Grouping
Sometimes you may need to create more advanced where
clauses such as "where exists" clauses or nested
parameter groupings. The Laravel query builder can handle
these as well. To get started, let's look at an example of
grouping constraints within parenthesis
32
Ordering, Grouping, Limit, & Offset
Ordering, Grouping, Limit, & Offset
33
Ordering, Grouping, Limit, & Offset
34
35
Conditional Clauses
Sometimes you may want clauses to apply to a query only
when something else is true. For instance you may only
want to apply a where statement if a given input value is
present on the incoming request. You may accomplish this
using the when method:
36
Conditional Clauses
37
Inserts
38
Updates/Deletes
39
Increment/Decrement
40
Pagination
In other frameworks, pagination can be very painful. Laravel's
paginator is integrated with the query builder and Eloquent ORM
and provides convenient, easy-to-use pagination of database
results out of the box. The HTML generated by the paginator is
compatible with the Bootstrap CSS framework.
There are several ways to paginate items. The simplest is by
using the paginate method on the query builder or an Eloquent
query. The paginate method automatically takes care of setting
the proper limit and offset based on the current page being
viewed by the user. By default, the current page is detected by
the value of the page query string argument on the HTTP request.
Of course, this value is automatically detected by Laravel, and is
also automatically inserted into links generated by the paginator.
41
Pagination/Simple Pagination
42
Displaying Pagination Results
43
Customize the Pagination View
By default, the views rendered to display the
pagination links are compatible with the
Bootstrap CSS framework. However, if you are
not using Bootstrap, you are free to define your
own views to render these links. When calling
the link method on a paginator instance, pass
the view name as the first argument to the
method:
44
Paginator Instance Methods
Object Oriented Programming with Laravel - Session 5

More Related Content

PDF
Object Oriented Programming with Laravel - Session 4
PDF
Object Oriented Programming with Laravel - Session 6
PDF
Object Oriented Programming with Laravel - Session 2
PPTX
Sql Injection attacks and prevention
DOCX
Asynchronous reading and writing http r equest
PPT
SQL injection basics
PPT
Entity frameworks101
PPT
Sql injection attacks
Object Oriented Programming with Laravel - Session 4
Object Oriented Programming with Laravel - Session 6
Object Oriented Programming with Laravel - Session 2
Sql Injection attacks and prevention
Asynchronous reading and writing http r equest
SQL injection basics
Entity frameworks101
Sql injection attacks

What's hot (19)

PPT
SQL Injection Attacks
PDF
SQL Injection Attack Detection and Prevention Techniques to Secure Web-Site
PPT
Defending against Injections
PDF
Oracle Concurrent Program Setup document
DOC
Asp.Net Database
PPTX
Web application penetration using SQLMAP.
PPT
D:\Technical\Ppt\Sql Injection
PPTX
PPT
Sql Injection Attacks Siddhesh
PPS
Manindra kishore _incident_handling_n_log_analysis - ClubHack2009
PPTX
Sql injection
PPT
SQL Injection
PPTX
SQL Injections - A Powerpoint Presentation
DOC
Asp.Net The Data List Control
PPTX
Koha Cataloguing Module
PPTX
Instant DBMS Homework Help
PDF
Assignment 2 - Power drill Grapevine "It's like Yik Yak, but for opinions ab...
PPTX
Using SP Metal for faster share point development
PPT
Rail3 intro 29th_sep_surendran
SQL Injection Attacks
SQL Injection Attack Detection and Prevention Techniques to Secure Web-Site
Defending against Injections
Oracle Concurrent Program Setup document
Asp.Net Database
Web application penetration using SQLMAP.
D:\Technical\Ppt\Sql Injection
Sql Injection Attacks Siddhesh
Manindra kishore _incident_handling_n_log_analysis - ClubHack2009
Sql injection
SQL Injection
SQL Injections - A Powerpoint Presentation
Asp.Net The Data List Control
Koha Cataloguing Module
Instant DBMS Homework Help
Assignment 2 - Power drill Grapevine "It's like Yik Yak, but for opinions ab...
Using SP Metal for faster share point development
Rail3 intro 29th_sep_surendran
Ad

Similar to Object Oriented Programming with Laravel - Session 5 (20)

PPTX
Laravel
PPTX
Introduction to laravel framework
PDF
Web Programming - 8 Database, Model and Eloquent
PDF
Getting to know Laravel 5
PPTX
laravel-53
DOCX
Laravel
PPTX
Laravel for Web Artisans
PDF
Best Laravel Eloquent Tips and Tricks
PPTX
Laravel - Website Development in Php Framework.
PPTX
Drupal 8 database api
PPTX
What-is-Laravel and introduciton to Laravel
PDF
Laravel tips-2019-04
PDF
Laravel Level 1 (The Basic)
PPTX
Drupal7 dbtng
PPTX
Laravel5 Introduction and essentials
PPTX
CRUD presentation of laravel application.pptx
PPTX
Laravel
PDF
Getting Started-with-Laravel
PDF
Getting started-with-laravel
PPTX
Frequently Asked Questions About Laravel
Laravel
Introduction to laravel framework
Web Programming - 8 Database, Model and Eloquent
Getting to know Laravel 5
laravel-53
Laravel
Laravel for Web Artisans
Best Laravel Eloquent Tips and Tricks
Laravel - Website Development in Php Framework.
Drupal 8 database api
What-is-Laravel and introduciton to Laravel
Laravel tips-2019-04
Laravel Level 1 (The Basic)
Drupal7 dbtng
Laravel5 Introduction and essentials
CRUD presentation of laravel application.pptx
Laravel
Getting Started-with-Laravel
Getting started-with-laravel
Frequently Asked Questions About Laravel
Ad

More from Shahrzad Peyman (10)

PDF
Web Design & Development - Session 9
PDF
Web Design & Development - Session 8
PDF
Web Design & Development - Session 7
PDF
Web Design & Development - Session 6
PDF
Web Design & Development - Session 4
PDF
Web Design & Development - Session 3
PDF
Web Design & Development - Session 2
PDF
Web Design & Development - Session 1
PDF
Object Oriented Programming with Laravel - Session 3
PDF
Object Oriented Programming with Laravel - Session 1
Web Design & Development - Session 9
Web Design & Development - Session 8
Web Design & Development - Session 7
Web Design & Development - Session 6
Web Design & Development - Session 4
Web Design & Development - Session 3
Web Design & Development - Session 2
Web Design & Development - Session 1
Object Oriented Programming with Laravel - Session 3
Object Oriented Programming with Laravel - Session 1

Recently uploaded (20)

PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
System and Network Administraation Chapter 3
PPTX
ai tools demonstartion for schools and inter college
PPTX
FLIGHT TICKET RESERVATION SYSTEM | FLIGHT BOOKING ENGINE API
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
AIRLINE PRICE API | FLIGHT API COST |
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
L1 - Introduction to python Backend.pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Transform Your Business with a Software ERP System
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
PPT
Introduction Database Management System for Course Database
Upgrade and Innovation Strategies for SAP ERP Customers
A REACT POMODORO TIMER WEB APPLICATION.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
System and Network Administraation Chapter 3
ai tools demonstartion for schools and inter college
FLIGHT TICKET RESERVATION SYSTEM | FLIGHT BOOKING ENGINE API
How Creative Agencies Leverage Project Management Software.pdf
AIRLINE PRICE API | FLIGHT API COST |
ISO 45001 Occupational Health and Safety Management System
ManageIQ - Sprint 268 Review - Slide Deck
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
L1 - Introduction to python Backend.pptx
PTS Company Brochure 2025 (1).pdf.......
Odoo POS Development Services by CandidRoot Solutions
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Transform Your Business with a Software ERP System
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
Introduction Database Management System for Course Database

Object Oriented Programming with Laravel - Session 5

  • 2. 2 Today’s Presentation • DataBase • Configuration • Multiple Connection • Raw Queries • Query Builder • Pagination
  • 3. 3 DataBase Laravel makes interacting with databases extremely simple across a variety of database backends using either raw SQL, the fluent query builder, and the Eloquent ORM. Currently, Laravel supports four databases: • MySQL • Postgres • SQLite • SQL Server
  • 4. Configuration 4 The database configuration for your application is located at config/database.php. In this file you may define all of your database connections, as well as specify which connection should be used by default. Examples for most of the supported database systems are provided in this file.
  • 5. Using Multiple DataBase Connection 5 When using multiple connections, you may access each connection via the connection method on th DB facade. The name passed to the connection method should correspond to one of the connections listed in your config/ database.php configuration file:
  • 6. 6 Running Raw SQL Queries Once you have configured your database connection, you may run queries using the DB facade. The DB facade provides methods for each type of query: select, update, insert, delete, and statement.
  • 8. 8 Running a Select Query The first argument passed to the select method is the raw SQL query, while the second argument is any parameter bindings that need to be bound to the query. Typically, these are the values of the where clause constraints. The select method will always return an array of results.
  • 10. Running a General Statement 10 Some database statements do not return any value. For these types of operations, you may use the statement method on the DB facade:
  • 11. DataBase Transactions You may use the transaction method on the DB facade to run a set of operations within a database transaction. If an exception is thrown within the transaction Closure, the transaction will automatically be rolled back. If the Closure executes successfully, the transaction will automatically be committed. You don't need to worry about manually rolling back or committing while using the transaction method:
  • 13. Query Builder Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works on all supported database systems.
  • 14. Retrieving All Rows From A Table 14
  • 15. 15 The get method returns an IlluminateSupportCollection containing the results where each result is an instance of the PHP StdClass object. You may access each column's value by accessing the column as a property of the object: Retrieving All Rows From A Table
  • 16. Retrieving A Single Row / Column From A Table 16
  • 17. Retrieving A List Of Column Values 17
  • 18. Aggregates 18 The query builder also provides a variety of aggregate methods such as count, max, min, avg, and sum. You may call any of these methods after constructing your query:
  • 20. 20 Raw Expressions Sometimes you may need to use a raw expression in a query. These expressions will be injected into the query as strings, so be careful not to create any SQL injection points! To create a raw expression, you may use the DB:raw method:
  • 21. 21 Inner Join The query builder may also be used to write join statements. To perform a basic "inner join", you may use the join method on a query builder instance. The first argument passed to the join method is the name of the table you need to join to, while the remaining arguments specify the column constraints for the join. Of course, as you can see, you can join to multiple tables in a single query:
  • 22. 22 Left Join If you would like to perform a "left join" instead of an "inner join", use the leftJoin method. The leftJoin method has the same signature as the join method:
  • 23. Cross Join 23 To perform a "cross join" use the crossJoin method with the name of the table you wish to cross join to. Cross joins generate a cartesian product between the first table and the joined table:
  • 25. 25 Unions The query builder also provides a quick way to "union" two queries together. For example, you may create an initial query and use the union method to union it with a second query: 25
  • 26. 26 Where Clauses You may use the where method on a query builder instance to add where clauses to the query. The most basic call to where requires three arguments. The first argument is the name of the column. The second argument is an operator, which can be any of the database's supported operators. Finally, the third argument is the value to evaluate against the column.
  • 31. 31 Parameters Grouping Sometimes you may need to create more advanced where clauses such as "where exists" clauses or nested parameter groupings. The Laravel query builder can handle these as well. To get started, let's look at an example of grouping constraints within parenthesis
  • 35. 35 Conditional Clauses Sometimes you may want clauses to apply to a query only when something else is true. For instance you may only want to apply a where statement if a given input value is present on the incoming request. You may accomplish this using the when method:
  • 40. 40 Pagination In other frameworks, pagination can be very painful. Laravel's paginator is integrated with the query builder and Eloquent ORM and provides convenient, easy-to-use pagination of database results out of the box. The HTML generated by the paginator is compatible with the Bootstrap CSS framework. There are several ways to paginate items. The simplest is by using the paginate method on the query builder or an Eloquent query. The paginate method automatically takes care of setting the proper limit and offset based on the current page being viewed by the user. By default, the current page is detected by the value of the page query string argument on the HTTP request. Of course, this value is automatically detected by Laravel, and is also automatically inserted into links generated by the paginator.
  • 43. 43 Customize the Pagination View By default, the views rendered to display the pagination links are compatible with the Bootstrap CSS framework. However, if you are not using Bootstrap, you are free to define your own views to render these links. When calling the link method on a paginator instance, pass the view name as the first argument to the method: