LogoLogo
6.4.0
6.4.0
  • Introduction
  • What's New?
  • Installation & Usage
  • Migration Guide
  • Contributing & Filing Issues
  • Contributors
  • Query Builder
    • Return Format
    • Interception Points
    • Retrieving Results
    • Aggregates
    • Selects
    • From
    • Joins
    • Where Clauses
    • Conditionals
    • Ordering, Grouping & Limit
    • Unions
    • Common Table Expressions (i.e. CTEs)
    • Inserts
    • Updates
    • Deletes
    • New Query
    • Clone
  • Schema Builder
    • Overview
    • Create
    • Columns
    • Column Modifiers
    • Column Constraints
    • Creating Table Constraints
    • Alter
    • Drop
Powered by GitBook
On this page
  • Basic if and else
  • when

Was this helpful?

Edit on Git
Export as PDF
  1. Query Builder

Conditionals

Basic if and else

If you store the builder object in a variable, you can use if and else statements like you would expect.

var builder = builder.from( "posts" );
if ( rc.recent ) {
    builder.orderBy( "published_date", "desc" );
}
var results = builder.get();

This works, but breaks chainability. A better way is to use the when helper method.

when

Name

Type

Required

Default

Description

condition

boolean

true

The condition to switch on.

onTrue

Closure

true

The callback to execute if the condition is true. It is passed the builder object as the only parameter.

onFalse

Closure

false

function( q ) { return q; }

The callback to execute if the conditions is false. It is passed the builder object as the only parameter.

We can rewrite the above query like so:

var results = builder.from( "posts" )
    .when( rc.recent, function( q ) {
        q.orderBy( "published_date", "desc" );
    } )
    .get();

Nice. We keep chainability this way and reduce the number of temporary variables we need.

PreviousWhere ClausesNextOrdering, Grouping & Limit

Last updated 7 years ago

Was this helpful?