SychO i do not understand this point
actually my serializer have
protected function getDefaultAttributes($model): array
{
return [
'id' => $model->id,
'userId' => $model->user_id,
// other fields
];
}
protected function user($model)
{
return $this->hasOne($model, UserSerializer::class, 'user');
}
my controller
class MyController extends AbstractListController
{
public $serializer = MySerializer::class;
public $sortFields = [
'createdAt'
];
protected $filterer;
protected $searcher;
protected $url;
public $limit = 20;
public $maxLimit = 20;
public function __construct(UserFilterer $filterer, UserSearcher $searcher, UrlGenerator $url)
{
$this->filterer = $filterer;
$this->searcher = $searcher;
$this->url = $url;
}
protected function data(ServerRequestInterface $request, Document $document)
{
$actor = RequestUtil::getActor($request);
$filters = $this->extractFilter($request);
$sort = $this->extractSort($request);
$sortIsDefault = $this->sortIsDefault($request);
if (! $actor->can('doSomething')) {
throw new PermissionDeniedException();
}
$limit = $this->extractLimit($request);
$offset = $this->extractOffset($request);
$include = $this->extractInclude($request);
$total = MyModel::all()->count();
$query = MyModel::with($include)
->skip($offset)
->take($limit)
->get();
$criteria = new QueryCriteria($actor, $filters, $sort, $sortIsDefault);
if (array_key_exists('q', $filters)) {
$results = $this->searcher->search($criteria, $limit, $offset);
} else {
$results = $this->filterer->filter($criteria, $limit, $offset);
}
$document->addPaginationLinks(
$this->url->to('api')->route('endpoint.list'),
$request->getQueryParams(),
$offset,
$limit,
$results->areMoreResults() ? null : 0
);
$results = $results->getResults();
$this->loadRelations($results, $include, $request);
$document->setMeta(
[
'total' => $total ,
'totalPages' => ceil($total / $limit)
]
);
$this->loadRelations($query, $include, $request);
return $query;
}
}
i get all the results correclty but there's no user relation
in extend.php i have
(new Extend\Model(User::class))
->relationship('mystring', function (User $user) {
return $user->hasMany(MyModel::class);
}),
(new Extend\Model(MyModel::class))
->belongsTo('user', User::class, 'user_id'),