BaseModel::all

Usage

ModelIterator BaseModel::all([ string $sort_by = 'id' ], [ string $sort_dir = 'asc' ], [ int $limit = '0' ], [ int $offset = '0' ])

Description

Get all records from the database table assigned to the model class.

Parameters

Parameter Required Type Description
$sort_by No string The column name to sort by. Defaults to the primary key column of the table.
$sort_dir No string The direction to sort by. Can be either `asc` or `desc`, and defaults to `asc`.
$limit No int The total number of records to return. Defaults to no limit, and returns all records.
$offset No int The offset at which to begin returning records. Defaults to 0.

Return Value

Returns a `ModelIterator` instance of all models / records that are returned.

Examples

Get All Categories Sorted by Name


use App\Demo\Models\Category; // Get all categories sorted by name $cats = Category::all('name'); foreach ($cats as $cat) { print_r($cat->toArray()); }

Get Products Sorted by Price Records 10 - 25


use App\Demo\Models\Product; // Get products 10 - 25 sorted by most expensive $products = Product::all('price', 'desc', 15, 10); foreach ($products as $product) { print_r($product->toArray()); }

See Also