BaseModel::whereFirst

Usage

?static BaseModel::whereFirst(string $where_sql, [ ... $args ])

Description

Get a model instance of the first record found for a given where clause.

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 The values of the placeholders within the $where_sql parameter.

Return Value

An instance of the model class of the first record found matching the where clause. If no record is found, returns null.

Examples

Get First Product


use App\Demo\Models\Product; // Get first product above 50 $price = 50; if (!$product = Product::whereFirst('is_active = %b AND price > %d', true, $Price)) { die("No products above 50"); } print_r9$product->toArray());

See Also