Closed
Description
Hey guys, I have been using the package for a while and love all the updates. One thing I am having problems with is the with function in the Adapter class. I believe this is supposed to be called on index and read, however I am only able to load the include relationships on an index call. This is causing a lot of N+1 queries in my application.
Here is my Adapter
<?php namespace App\JsonApi\Auctions;
use App\Models\Auction;
use Carbon\Carbon;
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
use CloudCreativity\LaravelJsonApi\Store\EloquentAdapter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
class Adapter extends EloquentAdapter
{
/**
* Adapter constructor.
* @param StandardStrategy $paging
*/
public function __construct(StandardStrategy $paging)
{
parent::__construct(new Auction(), $paging);
}
/**
* Apply the supplied filters to the builder instance.
*
* @param Builder $query
* @param Collection $filters
* @return void
*/
protected function filter(Builder $query, Collection $filters)
{
if (!$filters->has('ended')) {
$query->where('ends_at', '>', Carbon::now('UTC'));
}
$query->orderBy('ends_at', 'asc');
}
protected function with(Builder $query, Collection $includePaths)
{
if($includePaths->contains('location')) {
$query->with('location');
}
}
/**
* Is this a search for a singleton resource?
*
* @param Collection $filters
* @return bool
*/
protected function isSearchOne(Collection $filters)
{
return false;
}
}
And my schema
<?php namespace App\JsonApi\Auctions;
use Carbon\Carbon;
use CloudCreativity\LaravelJsonApi\Schema\EloquentSchema;
class Schema extends EloquentSchema
{
protected $resourceType = "auctions";
protected $attributes = [
'name',
'removal',
'highlights',
'notes',
'terms',
'contact',
'ends_at',
];
protected $dateFormat = Carbon::ISO8601;
/**
* @param object $resource
* @param bool $isPrimary
* @param array $includeRelationships
* @return array
*/
public function getRelationships($resource, $isPrimary, array $includeRelationships)
{
$return = [];
if (isset($includeRelationships['items'])) {
$return['items'] = [
self::SHOW_SELF => true,
self::SHOW_RELATED => true,
self::DATA => $resource->items,
];
}
$return['location'] = [
self::SHOW_SELF => true,
self::SHOW_RELATED => true,
self::DATA => isset($includeRelationships['location']) ? $resource->location : $this->createBelongsToIdentity($resource, 'location'),
];
return $return;
}
}