Skip to content

Query statements

Jeroen Grasdijk edited this page Jun 27, 2017 · 4 revisions

The following functions are added to select your models (more comming):

  • where(key, action, value)

    action: '=', '!=', '<', '>'

  • orWhere(key, action, value)

  • whereIn(key, values) / whereNotIn(key, values)

    values: [1, 2, ...]

  • whereBetween(key, min, max) / whereNotBetween(key, min, max)

  • whereNull(key) / whereNotNull(key)

  • whereHas(relation) / whereNotHas(relation)

  • orderBy(key, order)

    order 'asc' / 'desc'

*They are placed before one of the retrieving functions:

taskRds
    .where('id', '<', 3)
    .orderBy('id', 'desc')
    .get(); 

The where() and orWhere() function also except a callback function on which all the where functions can be applied:

taskRds
    .where((callback: WhereCallback) => { callback
        .where('id', '<', 3).whereNotNull('name')... 
    })
    .get(); 

The whereHas() function also accepts a callback function as second argument:

taskRds
    .whereHas('RelationName', (callback: WhereHasCallback) => { callback
        .where('id', '<', 3).whereNotNull('name')... 
    })
    .get(); 

All the where functions can be used in combination with the update() or remove() functions:

taskRds.where('id', '=', 1).remove(); // will only remove the task with id 1