You can return rows from the database by building queries programatically and format the output using these methods.
row( int $id | array $conditions )
Return a single row. Optionally include row id similar to the method rowId()
.
$user = $db->table('users')->row();
{
"id": "1",
"firstName": "Hans",
"lastName": "Gruber",
"email": "hans@gruber.com"
}
rowId( int $id )
Alias of row().
list( [string $column] )
Return multiple rows. Include optional column name as the row index.
$users = $db->table('users')->where(['id : >' => 1])->list();
[
{
"id": "2",
"firstName": "Lars",
"lastName": "Ulrich",
"email": "lars@metal.com"
},
{
"id": "3",
"firstName": "Hans",
"lastName": "Nickerdorph",
"email": "hans@nick.com"
}
]
listId( [default $column = 'id' ] )
Alias of list('id'). Default key is id
.
Use column name "email" to return multiple rows into array indexed by column value.
$users = $db->table('users')->listId('email');
{
"lars@metal.com": {
"id": "2",
"firstName": "Lars",
"lastName": "Ulrich",
"email": "lars@metal.com",
"created": "2020-12-24"
},
"hans@nicksport.com": {
"id": "3",
"firstName": "Hans",
"lastName": "Nickerdorph",
"email": "hans@nicksport.com",
"created": "2020-12-28"
}
}
groupId( [$column = 'id'] )
Return multiple rows into array grouped by column values. Default column is id
.
$users = $db->table('users')->groupId('category');
{
"client": [
{
"id": "1",
"firstName": "Hans",
"lastName": "Gruber",
"email": "hans@gruber.com",
"category": "client",
"created": null
},
{
"id": "3",
"firstName": "Hans",
"lastName": "Nickerdorph",
"email": "hans@nicksport.com",
"category": "client",
"created": "2020-12-28"
}
],
"partner": [
{
"id": "2",
"firstName": "Lars",
"lastName": "Ulrich",
"email": "lars@metal.com",
"category": "partner",
"created": "2020-12-24"
}
]
}
Last updated