laravel-api-handler
laravel-api-handler copied to clipboard
Proposal - Appends
trafficstars
It would be nice to add ability to make appends on results. For example if user model has getFullNameAttribute() we could use something like:
/users?_appends=fullName
I make workaround in my Repository Base Class (parserResult is method from Repository library and its called with ->all()):
/**
* @param array $fullTextSearchColumns Columns to search in fulltext search
* @param array|boolean $queryParams A list of query parameter
* @return $this
*/
public function scopeQueryParametersMultiple($fullTextSearchColumns = array(), $queryParams = false)
{
if ($queryParams === false) {
$queryParams = request()->except('_appends');
}
return $this->scopeQuery(function($query) use ($fullTextSearchColumns, $queryParams) {
return ApiHandler::parseMultiple($query, $fullTextSearchColumns, $queryParams)->getBuilder();
});
}
public function scopeQueryParametersSingle()
{
return $this->scopeQuery(function($query) {
return ApiHandler::parseSingle($query, [])->getBuilder();
});
}
/**
* Wrapper result data
*
* @param mixed $result
*
* @return mixed
*/
public function parserResult($result)
{
$result = parent::parserResult($result);
$appends = request()->filled('_appends') ? explode(',', request('_appends')) : null;
if ($appends) {
if ($result instanceof Collection) {
$result->each(function($row) use ($appends) {
$row->setAppends($appends);
});
} elseif ($result instanceof Model) {
$result->setAppends($appends);
}
}
return $result;
}
But would be nice to transfer this logic to Your library.
The upcoming v1.0 and Laravel resources should leave you with way more control over this than the current release. I hope to get this done soon.