Closed SuryaVanLierde closed 3 years ago
How are you using the relationship in your query?
Thanks for the quick response
Pretty basic really
$series = $publisher->series();
where $publisher
ins an instance of Publisher
received from the request
Please try this:
$publisher->series()->toBase()->select('series.*')->groupBy('series.id')->get();
That works, to some extent. It means I now get all the records I want, but as stdClass
, not as model instances, which means the eager loaded translations are no longer part of the result set. Any way of writing this and still getting the model instances?
The main problem here is that the Series
need to be sorted by title, but the title is a relation as some series are available in multiple languages.
class Series extends Model
{
use HasRelationships;
protected $appends = [
'title_display',
];
I also plan on using paginate()
on the query
I actually wanted to suggest this query:
$publisher->series()->getQuery()->select('series.*')->groupBy('series.id')->get();
groupBy()
works with pagination:
$publisher->series()->groupBy('series.id')->paginate();
Perfect. Thanks so much for your help given that my problem was not strictly with your package.
I just wish some of these functions you are using like getQuery
and toBase
would be documented, as they clearly are useful.
Models and relations
And of course there is a
Series
model.The problem is that the
series
relation on thePublisher
is returning eachSeries
as many times as there is anEdition
for thatPublisher
in theSeries
How do I trim it down so each series is only returned once?
A simple
groupBy('series.id')
does not work due to the full group by limitations, and I'm not sure how to get around this using the has many deep package.