Queries come in many varieties—from the basic to extremely complex. In order to provide you maximum flexibility there are several ways to define the source table for you query.
from()
methodThe most common method for defining the source table is using the from()
method. For the majority of queries, the from()
method is all you need. It's syntax is very easy:
This would return all columns from the users
table.
NOTE: Alternatively, you can use the
table()
method as an alias tofrom()
.
Optionally you can specify an alias for the table by using the syntax:
This would parse the string users as u
and convert it into the correct syntax for current grammar.
Alternatively, you can use the ANSI SQL shorthand and leave out the as
keyword:
from
clause using raw SQLSometimes you need more control over your from
clause in order to add grammar specific instructions, such as adding SQL Server table hints to your queries.
If you need complete control over your from
clause you can use the fromRaw()
.
For example, to provide a table hint for a SQL Server query you could use:
Since the fromRaw()
takes your string verbatim, it's important that you make sure your SQL declaration is escaped properly. Failure to properly escape your table names may result in SQL errors.
NOTE: Using the
fromRaw()
will most likely tie your code to a specific database, so think carefully before using thefromRaw()
method if you want your project to be database agnostic.
from
clauseMany database engines allow you to define User Defined Functions. For example, SQL Server allows you to define UDFs that will return a table. In these type of cases, it may be necessary to bind parameters to your from
clause.
You can bind parameters to the fromRaw()
method by passing a secondary argument that is an array of the parameters to bind:
Complex queries often contain derived tables. Derived tables are essentially a temporal table defined as a subquery in the from
statement.
You can build queries that comprise of derived tables by using the fromSub()
method, which requires two arguments:
The alias to use for the derived table (which is how you reference your query)
Either a QueryBuilder instances or closure defining the subquery
The simplest way to create a derived table is by using a closure to define the subquery:
Alternatively you can supply a QueryBuilder instance to the fromSub()
method: