Database
Custom Queries
PORM also allows you to run custom queries. You can do this by calling the raw
method. The raw
method takes the SQL query as the first argument and the bindings as the second argument.
In this raw queries, from
is required to initialise the query but the passed table name is not used in the query.
Transactions
Handling transactions here can be done in two ways.
Using the inTransaction
method
You can start a transaction by calling the inTransaction
method. The inTransaction
method takes a closure as the first argument. The closure should contain the queries to run in the transaction.
Line
4
defines the table name but this is just a formality. The table name is not used in the transaction queries.
To access the data from the transaction outside the transaction, you can use the use
keyword on the closure.
This takes care of committing and rolling back of the transactions automatically.
Using the pdo transactions
While the above method will do everything for you, we understand that you might want to have more control over the transactions. You can do this by using the pdo
method.
If you want to rollback the transaction, you can call the rollBack
method.
Advanced Filtering
Using the where
method
You can perform advanced filters using the where
method. You can chain as many of these as you want. You can use this method with both all
and get
methods.
All conditions that passed as array conditions are treated as AND
conditions.
Using the filter
method
In Porm, whenever you call the filter
you teleport to a new query builder that enables you to perform more complex queries.
Using the Where
Instance
You can also use the Where
instance to perform more complex queries.
Where.add() method can be used to add more conditions to the query.
The add()
method can also take up an entire Where
instance.
You can also use or()
method to add OR
conditions.
If you notice the return type of the build() method, it is an array
. This makes this more powerful as you can pass the Where
instance to other methods that accept an array of conditions.
Limit and Offset - Pagination
You can limit the number of results returned by using the limit
method. The limit
method takes the number of results to return as the first argument.
These are available on the filter
method.
You can also use the startAt
method to skip the first n
results.
Order By
You can order the results by using the orderBy
method. The orderBy
method takes the column to order by as the first argument and the order as the second argument.
You can order in two ways:
By column name :-
By one or more columns and the order :-
Grouping
You can group the results by using the group
method. The group
method takes the column(s) to group by as the first argument.
You can also group by multiple columns.
Having
You can filter the grouped results by using the having
method. The having
method takes the column to filter by as the first argument and the condition as the second argument.
You can also use any of the operators as the third argument.