Description
Hi,
I'm trying to use queriesOne
for a resource but can't seem to get it right. To cut to the chase, this is the error I'm currently getting:
No JSON API resource type registered for PHP class Illuminate\Database\Eloquent\Builder
Now more about the setup: I've got some time entries which are periods of time recorded by workers (aka "punches"). These time entries have a worker ID as well as a task ID.
Now these workers can have different hourly rates and the applied one can differ based on the nature of the task. So I've got another table task_workers
connecting the workers to tasks, and that table also has a rate ID.
This is roughly what it looks like:
Workers
id
Tasks
id
TimeEntries
worker_id
task_id
TaskWorkers
worker_id
task_id
rate_id
Rates
id
What I'm trying to achieve here, is the possibility to include the rate when querying the time entries, as a has-one relationship.
As you can see, using a regular Eloquent relationship is tricky (impossible, I think), because there is no direct link between time entries and task workers.
The query to fetch the rate from the TimeEntry
model class is fairly straightforward, however:
/**
* The model's rate (query builder).
*
* @return Builder
*/
public function rateQuery(): Builder
{
return Rate::query()
->join('task_workers', function (JoinClause $join) {
$join->on('rates.id', '=', 'task_workers.rate_id')
->where([
'task_workers.task_id' => $this->task_id,
'task_workers.worker_id' => $this->worker_id,
]);
});
}
This is the query I'm currently trying to use with the queriesOne
relationship. And this is the corresponding method from the time entry's adapter:
/**
* Return the QueriesOne relationship for the rate.
*
* @return QueriesOne
*/
protected function rate(): QueriesOne
{
return $this->queriesOne(function (TimeEntry $entry) {
return $entry->rateQuery();
});
}
I also have the following value for $includePaths
at the top, to avoid eager loading errors:
protected $includePaths = ['rate' => null];
I also added rate
to the list of authorised includes in the validators file.
And I get the error mentioned at the beginning when I try querying the endpoint:
No JSON API resource type registered for PHP class Illuminate\Database\Eloquent\Builder
My guess is I'm using queriesOne
in a wrong way, but I can't quite put my finger on it. Can you see what's wrong?
Cheers,
Yannick