Running Database Queries
Starting a query chain
To start fetching records, you can begin a chain a number of different ways. Each will start a query builder chain that you can then build out.Get all rows
Get a single row
If you are only wanting to select a single row from the database table, you have a few different options. To select the first row only from a query, use thefirst method.
find method. This will return the first record matching the criteria.
Select
Picking columns to return
Sometimes you may want to select just a subset of columns to return. While thefind and get methods can take a list of columns to limit down to, you can always explicitly call select.
Joins
You can easily join data from separate tables using the query builder. Thejoin method needs the table you are joining, and a clause to match up the data. If for example you are wanting to join all of a users order data, you could do the following:
leftJoin, rightJoin and crossJoin that you can use that take the same basic parameters.
Where Clauses
Basic Where Clauses
If you are wanting to filter down your results this can be done by using thewhere method. You can add as many where clauses to your query to continually filter down as far as needed. The simplest usage is to construct a WhereValue clause using some of the common operators. To do this, you would pass a column, the operator and then the value. For example if you wanted to get all users over 20 years old, you could do so as follows:
WhereValue in this way: ==, !=, <, >, <=, >=, ~=.
Alternatively you can manually create a WhereValue clause manually:
Or Where Clauses
By default chaining where clauses will be joined together using theand operator. If you ever need to switch the operator to or you can do so by using the orWhere method.
Grouping Where Clauses
If you need to group where clauses together, you can do so by using a closure. This will execute those clauses together within parenthesis to achieve your desired logical grouping.Additional Where Clauses
There are some additional helper where methods available for common cases. All methods also have a correspondingor method as well.
Where Null
ThewhereNull method ensures that the given column is not null.
Where In
Thewhere(key: String, in values [Parameter]) method lets you pass an array of values to match the column against.
Ordering, Grouping, Paging
Grouping
To group results together, you can use thegroupBy method:
having method which performs similar to a where clause.
Ordering
You can sort results of a query by using theorderBy method.
orderBy as many times as needed. Sorting is based on call order.
Paging, Limits and Offsets
If all you are looking for is to break a query down into chunks for paging, the easiest way to accomplish that is to use theforPage method. It will automatically set the limits and offsets appropriate for a page size you define.
Inserting
You can insert records using the query builder as well. To do so, start a chain with only a table name, and then pass the record you wish to insert. You can additionally pass in an array of records to do a bulk insert.Updating
Updating records is just as easy as inserting, however you also get the benefit of the rest of the query builder chain. Any where clauses that have been added are used to match which records you want to update. For example, if you wanted to update a single user based on an ID, you could do so as follows:Deleting
Thedelete method works similar to how update did. It uses the query builder chain to determine what records match, but then instead of updating them, it deletes them. If you wanted to delete all users whose name is Peter, you could do that as so:
Counting
To get the total number of records that match a query you can use thecount method.

