Skip to content

QueryResult Class

The QueryResult class represents the result of a database query. It provides methods to access the items retrieved by the query, the row count, and other helpful utilities to work with the query result.


getItems Method

Retrieves the items returned by the query.

php
getItems(): array|object

Return:

  • array|object: Returns the items retrieved by the query. This can be an array or an object depending on how the query results were fetched.

Example:

php
$items = Sparkle::table('orders')->limit(5)->get()->getItems();

getRowCount Method

Gets the number of rows returned by the query.

php
getRowCount(): int

Return:

  • int: Returns the number of rows that were returned by the query.

Example:

php
$rowCount = Sparkle::table('orders')->limit(5)->get()->getRowCount();

toArray Method

Converts the query result to an associative array that includes both the row count and the items.

php
toArray(): array

Return:

  • array: Returns an associative array with the following keys:
    • rowCount: The number of rows returned by the query.
    • items: The query result converted into an array format.

Example:

php
$array = Sparkle::table('orders')->limit(5)->get()->toArray();

hasItems Method

Checks if the result contains any items.

php
hasItems(): bool

Return:

  • bool: Returns true if the result contains items (i.e., the row count is greater than 0), otherwise returns false.

Example:

php
if (Sparkle::table('orders')->limit(5)->get()->hasItems()) {
    echo "There are items!";
} else {
    echo "No items found!";
}

Released under the MIT License.