Getter(QueryBuilder15)


Getter

The Db class can also support getter definitions, for example:

Db::name('user')->withAttr('name', function($value, $data) {
	return strtolower($value);
})->select();

The getter method supports incoming Two parameters, the first parameter is the value of the current field, and the second parameter is all the data.

In the above code, the value of the name field in the queried data set will be uniformly converted to lowercase.

The withAttr method can be called multiple times to define getters for multiple fields.

Supports defining getters for JSON fields, for example:

$user = Db::name('user')
	->json(['info'])
    ->withAttr('info.name', function($value, $data) {
        return strtolower($value);
    })->find(1);
dump($user);

When the query results are returned, the getter operation will be automatically used on the name attribute of the info field (JSON field).