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)

PDF
Object Oriented Programming with Laravel - Session 3
PPTX
DOCX
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
PDF
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
PPTX
SCWCD : The servlet model : CHAP : 2
PPT
Ruby On Rails Siddhesh
PPTX
ADO.NET by ASP.NET Development Company in india
PPTX
GraphQL API Gateway and microservices
PPT
Migrating from PHP 4 to PHP 5
PPTX
Chapter 07 - Fundamentals of dotnet.pptx
PPTX
SCWCD : Handling exceptions : CHAP : 5
PPT
Lect_04b_PhpMysqlKEY PERFORMANCE INDICATOR FOR ICT-UNIT (new).ppt
PDF
Modern Database Management 11th Edition Hoffer Solutions Manual
PPTX
SCWCD : The servlet model CHAP : 2
PPT
Php and MySQL Web Development
PPT
ASP.NET 09 - ADO.NET
PDF
WEB-MODULE 4.pdf
PDF
How to Fine-Tune Performance Using Amazon Redshift
PPTX
Database novelty detection
PPT
Ebook7
Object Oriented Programming with Laravel - Session 3
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
SCWCD : The servlet model : CHAP : 2
Ruby On Rails Siddhesh
ADO.NET by ASP.NET Development Company in india
GraphQL API Gateway and microservices
Migrating from PHP 4 to PHP 5
Chapter 07 - Fundamentals of dotnet.pptx
SCWCD : Handling exceptions : CHAP : 5
Lect_04b_PhpMysqlKEY PERFORMANCE INDICATOR FOR ICT-UNIT (new).ppt
Modern Database Management 11th Edition Hoffer Solutions Manual
SCWCD : The servlet model CHAP : 2
Php and MySQL Web Development
ASP.NET 09 - ADO.NET
WEB-MODULE 4.pdf
How to Fine-Tune Performance Using Amazon Redshift
Database novelty detection
Ebook7
Ad

More from Shahrzad Peyman (9)

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

Recently uploaded (20)

PPTX
L1 - Introduction to python Backend.pptx
PDF
System and Network Administration Chapter 2
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Transform Your Business with a Software ERP System
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Nekopoi APK 2025 free lastest update
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
PPTX
history of c programming in notes for students .pptx
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Digital Strategies for Manufacturing Companies
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
L1 - Introduction to python Backend.pptx
System and Network Administration Chapter 2
Design an Analysis of Algorithms I-SECS-1021-03
Transform Your Business with a Software ERP System
Reimagine Home Health with the Power of Agentic AI​
Understanding Forklifts - TECH EHS Solution
Nekopoi APK 2025 free lastest update
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
history of c programming in notes for students .pptx
Wondershare Filmora 15 Crack With Activation Key [2025
Digital Strategies for Manufacturing Companies
Upgrade and Innovation Strategies for SAP ERP Customers
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
How to Choose the Right IT Partner for Your Business in Malaysia
CHAPTER 2 - PM Management and IT Context
Navsoft: AI-Powered Business Solutions & Custom Software Development
Adobe Illustrator 28.6 Crack My Vision of Vector Design
How to Migrate SBCGlobal Email to Yahoo Easily

Object Oriented Programming with Laravel - Session 5

  • 1. Object-Oriented Programming (with Laravel) By: Shahrzad Peyman Session 5 May-2017 shahrzad.peymaan@gmail.com 1
  • 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: