BaseModel::getChildren

Usage

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

Description

Get child records based on a foreign key constraint. You will generally never call this method yourself, and instead it is included within methods to retrieve child rows upon model class generation.

Parameters

Parameter Required Type Description
$foreign_key Yes string The foreign key to retrieve child rows of, formatted as table_name.column_name
$class_name Yes string The fully qualified class name of the child model class.
$sort_by No string The column name to sort child records by. Defaults to the primary key column of the child table.
$sort_dir No string The direction to sort child records by. Can be either `asc` or `desc`, and defaults to `asc`.
$limit No int The maximum number of child records to return. Defaults to all child records.
$offset No int Where within the result set to begin retrieving child records. Defaults to 0.

Return Value

Returns a `ModelIterator` instance of all models / records of the child records.

Examples

Get All Products Within a Category


use App\Demo\Models\{Category, Product}; // Get category id# 14 $cat_id = 14; if (!$cat = Category::whereId($cat_id)) { die("No category exists with id# $cat_id"); } // Get all products $products = $cat->getChildren('products.category_id', Product::class); foreach ($products as $product) { print_r($product->toArray()); }

See Also