# Fluent Forms Query Builder

# Introduction

Fluent Forms 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.

Note: Our Query Builder is compatible the PHP Laravel Framework's Query Builder. If you are familiar with Laravel's Query Builder, you will feel right at home using the Fluent Forms Query Builder.

# Example

Here is an example Fluent Query Builder

$query = wpFluent()->table('fluentform_forms')
            ->select(['id', 'title', 'form_fields', 'type'])
            ->where('has_payment', 1)
            ->whereBetween('created_at', ['2022-12-30 00:00:00', '2023-12-30 23:59:59'])
            ->when($formId, function ($query) use ($formId) {
                return $query->where('id', $formId);
            })
            ->orderBy('id', 'ASC');
1
2
3
4
5
6
7
8

# Retrieving Results

# Retrieving All Rows From A Table

You may use the table method on the wpFluent()function to begin a query. Thetablemethod returns a fluent query builder instance for the given table, allowing you to chain more constraints onto the query and then finally get the results using theget` method:

<?php
 
namespace FluentForm\App\Http\Controllers;
 
class FormController extends Controller
{
    /**
     * Show a list of all fluent forms.
     *
     * @return Response
     */
    public function index()
    {
        $forms = wpFluent()->table('fluentform_forms')
                ->select(['id', 'title', 'form_fields', 'type'])
                ->get();
 
        return [
            'available_forms' => $forms
        ];   
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

The get method returns an array 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:

foreach ($forms as $form) {
    echo $form->title;
}
1
2
3

# Retrieving A Single Row / Column From A Table

If you just need to retrieve a single row from the database table, you may use the first method. This method will return a single stdClass object:

$form = wpFluent()->table('fluentform_forms')->where('title', 'LIKE' , '%Blank Form%')->first();
 
echo $form->title;
1
2
3

If you don't even need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly:

$title = wpFluent()->table('fluentform_forms')->where('title', 'LIKE' , '%Blank Form%')->value('title');
1

# Retrieving A List Of Column Values

If you would like to retrieve an array containing the values of a single column, you may use the pluck method. In this example, we'll retrieve an array of all form fields:

$formsFields = wpFluent()->table('fluentform_forms')->pluck('form_fields');
 
foreach ($formsFields as $formField) {
    $formField = \json_decode($formField, true);
    // do your stuff
}
1
2
3
4
5
6

You may also specify a custom key column for the returned Collection:

$formsFields = wpFluent()->table('fluentform_forms')->pluck('form_fields', 'id');
 
foreach ($formsFields as $id => $formField) {
    $formField = \json_decode($formField, true);
    // do your stuff
}
1
2
3
4
5
6

# Chunking Results

If you need to work with thousands of database records, consider using the chunk method. This method retrieves a small chunk of the results at a time and feeds each chunk into a Closure for processing. This method is very useful for process thousands of records. For example, let's work with the entire fluentform_forms table in chunks of 100 records at a time:

wpFluent()->table('fluentform_forms')->orderBy('id')->chunk(100, function ($forms) {
    foreach ($forms as $form) {
        //
    }
});
1
2
3
4
5

You may stop further chunks from being processed by returning false from the Closure:

wpFluent()->table('fluentform_forms')->orderBy('id')->chunk(100, function ($forms) {
    // Process the records...
    
    return false;
});
1
2
3
4
5

# Aggregates

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:

$totalForms = wpFluent()->table('fluentform_forms')->count();
 
$price = wpFluent()->table('fluentform_transaction')->max('payment_total');
1
2
3

Of course, you may combine these methods with other clauses:

$price = wpFluent()->table('fluentform_transaction')
                ->where('payment_methods', 'stripe')
                ->avg('payment_total');
1
2
3

# Determining If Records Exist

Instead of using the count method to determine if any records exist that match your query's constraints, you may use the exists:

return wpFluent()->table('fluentform_transaction')->where('payment_methods', 'stripe')->exists();
1

# Selects

# Specifying A Select Clause

Of course, you may not always want to select all columns from a database table. Using the select method, you can specify a custom select clause for the query:

$forms = wpFluent()->table('fluentform_forms')->select('title', 'fields as form_fields')->get();
1

The distinct method allows you to force the query to return distinct results:

$forms = wpFluent()->table('fluentform_forms')->distinct()->get();
1

If you already have a query builder instance and wish to add a column to its existing select clause, you may use the addSelect method:

$query = wpFluent()->table('fluentform_forms')->select('title');
 
$forms = $query->addSelect('form_fields')->get();
1
2
3

# Raw Expressions

Sometimes you may need to use a raw expression in a query. To create a raw expression, you may use the raw method:

$forms = wpFluent()->table('fluentform_forms')
                     ->select(wpFluent()->raw('count(*) as title, status'))
                     ->where('has_payment', 1)
                     ->groupBy('title')
                     ->get();
1
2
3
4
5

# Raw Methods

Instead of using wpFluent()->raw, you may also use the following methods to insert a raw expression into various parts of your query.

# selectRaw

The selectRaw method can be used in place of select(wpFluent()->raw(...)). This method accepts an optional array of bindings as its second argument:

$orders = wpFluent()->table('fluentform_order_items')
                ->selectRaw('item_price * ? as price_with_tax', [1.0825])
                ->get();
1
2
3

# whereRaw / orWhereRaw

The whereRaw and orWhereRaw methods can be used to inject a raw where clause into your query. These methods accept an optional array of bindings as their second argument:

$orders = wpFluent()->table('fluentform_order_items')
                ->whereRaw('item_price > IF(state = "TX", ?, 100)', [200])
                ->get();
1
2
3

# havingRaw / orHavingRaw

The havingRaw and orHavingRaw methods may be used to set a raw string as the value of the having clause. These methods accept an optional array of bindings as their second argument:

$orders = wpFluent()->table('fluentform_order_items')
                ->select('quantity', DB::raw('SUM(item_price) as total_sales'))
                ->groupBy('quantity')
                ->havingRaw('SUM(item_price) > ?', [2500])
                ->get();
1
2
3
4
5

# orderByRaw

The orderByRaw method may be used to set a raw string as the value of the order by clause:

$forms = wpFluent()->table('fluentform_forms')
                ->orderByRaw('updated_at - created_at DESC')
                ->get();
1
2
3

# Joins

# Inner Join Clause

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:

$submissions = wpFluent()->table('fluentform_submissions')
            ->join('users', 'users.id', '=', 'fluentform_submissions.user_id')
            ->select('fluentform_submissions.*', 'users.phone')
            ->get();
1
2
3
4

# left Join Clause

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:

$submissions = wpFluent()->table('fluentform_submissions')
            ->leftJoin('users', 'users.id', '=', 'fluentform_submissions.user_id')
            ->get();
1
2
3

# Cross Join Clause

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:

$submissions = wpFluent()->table('fluentform_submissions')
            ->crossJoin('users')
            ->get();
1
2
3

# Advanced Join Clauses

You may also specify more advanced join clauses. To get started, pass a Closure as the second argument into the join method. The Closure will receive a JoinClause object which allows you to specify constraints on the join clause:

wpFluent()->table('fluentform_submissions')
        ->join('users', function ($join) {
            $join->on('fluentform_submissions.user_id', '=', 'users.id')->orOn(...);
        })
        ->get();
1
2
3
4
5

If you would like to use a "where" style clause on your joins, you may use the where and orWhere methods on a join. Instead of comparing two columns, these methods will compare the column against a value:

wpFluent()->table('fluentform_submissions')
        ->join('users', function ($join) {
            $join->on('fluentform_submissions.user_id', '=', 'users.id')
                 ->where('users.id', '>', 5);
        })
        ->get();
1
2
3
4
5
6

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

$first = wpFluent()->table('users')
            ->whereNull('first_name');
 
$user = wpFluent()->table('users')
            ->whereNull('last_name')
            ->union($first)
            ->get();
1
2
3
4
5
6
7

# Where Clauses

# Simple 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. For example, here is a query that verifies the value of the "first_name" column is equal to 'John Smith':

$forms = wpFluent()->table('fluentform_forms')->where('title', '=', 'Blank Form')->get();
1

For convenience, if you want to verify that a column is equal to a given value, you may pass the value directly as the second argument to the where method:

$forms = wpFluent()->table('fluentform_forms')->where('title', 'Blank Form')->get();
1

Of course, you may use a variety of other operators when writing a where clause:

$orders = wpFluent()->table('fluentform_order_items')
                ->where('line_total', '>=', 1000)
                ->get();
 
$orders = wpFluent()->table('fluentform_order_items')
                ->where('line_total', '<>', 1000)
                ->get();
 
$forms = wpFluent()->table('fluentform_forms')
                ->where('title', 'like', 'T%')
                ->get();
1
2
3
4
5
6
7
8
9
10
11

You may also pass an array of conditions to the where function:

$forms = wpFluent()->table('fluentform_forms')->where([
    ['status', '=', 'published'],
    ['has_payment', '<>', '1'],
])->get();
1
2
3
4

# Or Statements

You may chain where constraints together as well as add or clauses to the query. The orWhere method accepts the same arguments as the where method:

$forms = wpFluent()->table('fluentform_forms')
                    ->where('form_fields', 'like', '%Blank Form%')
                    ->orWhere('title', 'Blank Form')
                    ->get();
1
2
3
4

# Additional Where Clauses

# whereBetween

The whereBetween method verifies that a column's value is between two values:

$orders = wpFluent()->table('fluentform_order_items')
             ->whereBetween('line_total', [1, 100])->get();
1
2

# whereNotBetween

The whereNotBetween method verifies that a column's value lies outside two values:

$orders = wpFluent()->table('fluentform_order_items')
             ->whereNotBetween('line_total', [1, 100])->get();
1
2

# whereIn / whereNotIn

The whereIn method verifies that a given column's value is contained within the given array:

$forms = wpFluent()->table('fluentform_forms')
                    ->whereIn('id', [1, 2, 3])
                    ->get();
1
2
3

The whereNotIn method verifies that the given column's value is not contained in the given array:

$forms = wpFluent()->table('fluentform_forms')
                    ->whereNotIn('id', [1, 2, 3])
                    ->get();
1
2
3

# whereNull / whereNotNull

The whereNull method verifies that the value of the given column is NULL:

$forms = wpFluent()->table('fluentform_forms')
                    ->whereNull('updated_at')
                    ->get();
1
2
3

The whereNotNull method verifies that the column's value is not NULL:

$forms = wpFluent()->table('fluentform_forms')
                    ->whereNotNull('updated_at')
                    ->get();
1
2
3

# whereDate / whereMonth / whereDay / whereYear / whereTime

The whereDate method may be used to compare a column's value against a date:

$forms = wpFluent()->table('fluentform_forms')
                ->whereDate('created_at', '2016-12-31')
                ->get();
1
2
3

The whereMonth method may be used to compare a column's value against a specific month of a year:

$forms = wpFluent()->table('fluentform_forms')
                ->whereMonth('created_at', '12')
                ->get();
1
2
3

The whereDay method may be used to compare a column's value against a specific day of a month:

$forms = wpFluent()->table('fluentform_forms')
                ->whereDay('created_at', '21')
                ->get();
1
2
3

The whereYear method may be used to compare a column's value against a specific year:

$forms = wpFluent()->table('fluentform_forms')
                ->whereYear('created_at', '2023')
                ->get();
1
2
3

The whereTime method may be used to compare a column's value against a specific time:

$forms = wpFluent()->table('fluentform_forms')
                ->whereTime('created_at', '11:20:45')
                ->get();
1
2
3

The whereTimestamp method may be used to compare a column's value against a specific time:

$forms = wpFluent()->table('fluentform_forms')
                ->whereTimestamp('created_at', '2023-11-21 11:20:45')
                ->get();
1
2
3

# whereColumn

The whereColumn method may be used to verify that two columns are equal:

$forms = wpFluent()->table('fluentform_forms')
                 ->whereColumn('conditions', 'appearance_settings')
                ->get();
1
2
3

You may also pass a comparison operator to the method:

$forms = wpFluent()->table('fluentform_forms')
                 ->whereColumn('updated_at', '>', 'created_at')
                ->get();
1
2
3

The whereColumn method can also be passed an array of multiple conditions. These conditions will be joined using the and operator:

$forms = wpFluent()->table('fluentform_forms')
                 ->whereColumn([
                    ['conditions', '=', 'appearance_settings'],
                    ['updated_at', '>', 'created_at']
                ])->get();
1
2
3
4
5

# Where Exists Clauses

The whereExists method allows you to write where exists SQL clauses. The whereExists method accepts a Closure argument, which will receive a query builder instance allowing you to define the query that should be placed inside the "exists" clause:

wpFluent()->table('fluentform_forms')
            ->whereExists(function ($query) {
                $query->select(wpFluent()->raw(1))
                      ->from('fluentform_order_items')
                      ->whereRaw('fluentform_order_items.form_id = fluentform_forms.id');
            })
            ->get();
1
2
3
4
5
6
7

The query above will produce the following SQL:

select * from fluentform_forms
where exists (
    select 1 from fluentform_order_items where fluentform_order_items.form_id = fluentform_forms.id
)
1
2
3
4

# Ordering, Grouping, Limit, & Offset

# orderBy

The orderBy method allows you to sort the result of the query by a given column. The first argument to the orderBy method should be the column you wish to sort by, while the second argument controls the direction of the sort and may be either asc or desc:

$forms = wpFluent()->table('fluentform_forms')
                 ->orderBy('created_at', 'DESC')
                ->get();
1
2
3

# latest / oldest

The latest and oldest methods allow you to easily order results by date. By default, result will be ordered by the created_at column. Or, you may pass the column name that you wish to sort by:

$forms = wpFluent()->table('fluentform_forms')
                 ->latest()
                ->get();
1
2
3

# inRandomOrder

The inRandomOrder method may be used to sort the query results randomly. For example, you may use this method to fetch a random user:

$forms = wpFluent()->table('fluentform_forms')
                 ->inRandomOrder()
                ->get();
1
2
3

# groupBy / having

The groupBy and having methods may be used to group the query results. The having method's signature is similar to that of the where method:

$forms = wpFluent()->table('fluentform_forms')
                ->groupBy('id')
                ->having('id', '>', 100)
                ->get();
1
2
3
4

You may pass multiple arguments to the groupBy method to group by multiple columns:

$forms = wpFluent()->table('fluentform_forms')
                ->groupBy('id', 'status')
                ->having('id', '>', 100)
                ->get();
1
2
3
4

# skip / take

To limit the number of results returned from the query, or to skip a given number of results in the query, you may use the skip and take methods:

$forms = wpFluent()->table('fluentform_forms')
                ->skip(10)
                ->take(5)
                ->get();
1
2
3
4

Alternatively, you may use the limit and offset methods:

$forms = wpFluent()->table('fluentform_forms')
                ->limit(10)
                ->offset(5)
                ->get();
1
2
3
4

# 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 when method:

$user = $request->get('user');
 
$submissions = wpFluent()->table('fluentform_submissions')
                ->when($user, function ($query, $user) {
                    return $query->where('user_id', $user);
                })
                ->get();
1
2
3
4
5
6
7

The when method only executes the given Closure when the first parameter is true. If the first parameter is false, the Closure will not be executed.

You may pass another Closure as the third parameter to the when method. This Closure will execute if the first parameter evaluates as false. To illustrate how this feature may be used, we will use it to configure the default sorting of a query:

$sortBy = null;
 
$forms = wpFluent()->table('fluentform_forms')
                ->when($sortBy, function ($query, $sortBy) {
                    return $query->orderBy($sortBy);
                }, function ($query) {
                    return $query->orderBy('title');
                })
                ->get();
1
2
3
4
5
6
7
8
9

# Inserts

The query builder also provides an insert method for inserting records into the database table. The insert method accepts an array of column names and values:

wpFluent()->table('fluentform_forms')->insert(
    ['title' => 'New Form', 'has_payment' => '1', 'status' => 'published', 'form_fields' => "[...field json data]"]
);
1
2
3

You may even insert several records into the table with a single call to insert by passing an array of arrays. Each array represents a row to be inserted into the table:

wpFluent()->table('fluentform_forms')->insert([
    ['title' => 'New Form', 'has_payment' => 0, 'status' => 'published', 'form_fields' => "[...field json data]"],
    ['title' => 'New Form 2', 'has_payment' => 0, 'status' => 'published', 'form_fields' => "[...field json data]"]
]);
1
2
3
4

# Auto-Incrementing IDs

If the table has an auto-incrementing id, use the insertGetId method to insert a record and then retrieve the ID:

$formId = wpFluent()->table('fluentform_forms')->insertGetId(
    ['title' => 'New Form', 'has_payment' => '1', 'status' => 'published', 'form_fields' => "[...field json data]"]
);
1
2
3

# Updates

Of course, in addition to inserting records into the database, the query builder can also update existing records using the update method. The update method, like the insert method, accepts an array of column and value pairs containing the columns to be updated. You may constrain the update query using where clauses:

wpFluent()->table('fluentform_forms')
            ->where('id', 1)
            ->update(['title' => 'New updated title']);
1
2
3

# Increment & Decrement

The query builder also provides convenient methods for incrementing or decrementing the value of a given column. This is a shortcut, providing a more expressive and terse interface compared to manually writing the update statement.

Both of these methods accept at least one argument: the column to modify. A second argument may optionally be passed to control the amount by which the column should be incremented or decremented:

wpFluent()->table('fluentform_order_items')->increment('quantity');
 
wpFluent()->table('fluentform_order_items')->increment('quantity', 5);
 
wpFluent()->table('fluentform_order_items')->decrement('quantity');
 
wpFluent()->table('fluentform_order_items')->decrement('quantity', 5);
1
2
3
4
5
6
7

You may also specify additional columns to update during the operation:

wpFluent()->table('fluentform_order_items')->increment('line_total', 1, ['item_price' => '99.99']);
1

# Deletes

The query builder may also be used to delete records from the table via the delete method. You may constrain delete statements by adding where clauses before calling the delete method:

wpFluent()->table('fluentform_order_items')->delete();
 
wpFluent()->table('fluentform_order_items')->where('line_total', '>', 100)->delete();
1
2
3

If you wish to truncate the entire table, which will remove all rows and reset the auto-incrementing ID to zero, you may use the truncate method:

wpFluent()->table('fluentform_order_items')->truncate();
1

# Pessimistic Locking

The query builder also includes a few functions to help you do "pessimistic locking" on your select statements. To run the statement with a "shared lock", you may use the sharedLock method on a query. A shared lock prevents the selected rows from being modified until your transaction commits:

wpFluent()->table('fluentform_order_items')->where('line_total', '>', 100)->sharedLock()->get();
1

Alternatively, you may use the lockForUpdate method. A "for update" lock prevents the rows from being modified or from being selected with another shared lock:

wpFluent()->table('fluentform_order_items')->where('line_total', '>', 100)->lockForUpdate()->get();
1