BaseModel::where

Usage

ModelIterator BaseModel::where(string $where_sql, [ ... $args ])

Description

Select multiple records from the database table that is assigned to the model class.

Parameters

Parameter Required Type Description
$where_sql Yes string The WHERE clause within the SQL statement. Can contain placeholders (eg. %s, %i, %b, et al) and also additional clauses such as ORDER BY, GROUP BY< LIMIT, OFFSET, et al.
$args No desc=

Return Value

Returns a `ModelIterator` instance of all models / records that match the where clause.

Examples

Select All Active Products in Electronics Category


use App\Demo\Models\Product; $slug = 'electronics'; $products = Product::where('category = %s AND is_active = %b', $slug, true); foreach ($products as $product) { print_r($product->toArray()); }

Using ORDER BY, LIMIT and OFFSET


use App\Demo\Models\Product; // Get products 10 - 25 ordered by price for category games $category_id = 12; $products = Product::where('is_active = %b AND category_id = %i ORDER BY price DESC LIMIT 15 OFFSET 10", true, $category_id); foreach ($products as $product) { print_r($product->toArray()); }

See Also