The Fluent Query Builder is Laravel's powerful fluent interface for building SQL queries and working with your database. All queries use prepared statements and are protected against SQL injection.
You can begin a fluent query using the **table** method on the DB class. Just mention the table you wish to query:
$query = DB::table('users');
You now have a fluent query builder for the "users" table. Using this query builder, you can retrieve, insert, update, or delete records from the table.
<a name="get"></a>
## Retrieving Records
#### Retrieving an array of records from the database:
$users = DB::table('users')->get();
> **Note:** The **get** method returns an array of objects with properties corresponding to the column on the table.
#### Retrieving a single record from the database:
$user = DB::table('users')->first();
#### Retrieving a single record by its primary key:
$user = DB::table('users')->find($id);
> **Note:** If no results are found, the **first** method will return NULL. The **get** method will return an empty array.
#### Retrieving the value of a single column from the database:
#### Selecting distinct results from the database:
$user = DB::table('users')->distinct()->get();
<a name="where"></a>
## Building Where Clauses
### where and or\_where
There are a variety of methods to assist you in building where clauses. The most basic of these methods are the **where** and **or_where** methods. Here is how to use them:
return DB::table('users')
->where('id', '=', 1)
->or_where('email', '=', 'example@gmail.com')
->first();
Of course, you are not limited to simply checking equality. You may also use **greater-than**, **less-than**, **not-equal**, and **like**:
return DB::table('users')
->where('id', '>', 1)
->or_where('name', 'LIKE', '%Taylor%')
->first();
As you may have assumed, the **where** method will add to the query using an AND condition, while the **or_where** method will use an OR condition.
### where\_in, where\_not\_in, or\_where\_in, and or\_where\_not\_in
The suite of **where_in** methods allows you to easily construct queries that search an array of values:
You may discover the need to group portions of a WHERE clause within parentheses. Just pass a Closure as parameter to the **where** or **or_where** methods:
Sometimes you may need to set the value of a column to a SQL function such as **NOW()**. Usually a reference to now() would automatically be quoted and escaped. To prevent this use the **raw** method on the **DB** class. Here's what it looks like:
The **raw** method tells the query to inject the contents of the expression into the query as a string rather than a bound parameter. For example, you can also use expressions to increment column values: