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.
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
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: