As said yesterday, I'll copy/paste the necessary parts of my code here for a better comprehension of my problem.
My mean issue is how to get the same stuff as the left column of the website I gave as example (https://bibliotheque.braille.be/fr/catalogue).
In my Eloquent Book Model, I have those relationships :
public function catalogs()
{
return $this->belongsToMany(Catalog::class)
->using(CatalogFrontendBook::class)
->withPivot('frontend_book_id', 'catalog_id', 'nbr_of_copies', 'new', 'newed_at', 'length')
->withTimestamps();
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function author()
{
return $this->belongsTo(Author::class);
}
public function serie()
{
return $this->belongsTo(Serie::class);
}
The search Method in my BookController :
/**
* Search Books
*
* @param BookServices $bookServices
* @param BookSearch $request
*
* @return mixed
*/
public function search(BookServices $bookServices, BookSearch $request)
{
$response = $bookServices->search($request, false);
// How to get those relations based on the whole results, not on the 12 results returned by the pagination,
// without running a foreach on $response->get() to crawl all results
// perhaps have I to do a raw query to get all joined tables ?
$catalogs = [];
$categories = [];
$authors = [];
$series = [];
$meta = [
'meta' => [
'catalogs' => $catalogs,
'categories' => $categories,
'authors' => $authors,
'series' => $series,
]
];
return (BookForListResource::collection($response->paginate($request->perPage ? $request->perPage : 12)))->additional($meta);
}
The Search method in the Book service :
/**
* Run Search for a book
*
* @param $request
* @param bool $auth
*
* @return mixed
*/
public function search($request, $auth = false)
{
$catalog = $request->catalog;
$category = $request->category;
$field = $request->field;
$term = $request->term;
// termSearch - catalogSearch - categorySearch are scopes in Book models
// here is the eager load of all required relations
$books = FrontendBook::with('author', 'serie', 'catalogs', 'category', 'subcategory')
->termSearch($term, $field)
->catalogSearch($catalog)
->categorySearch($category);
return $books;
}
And the Resource "BookForListResource" (Json\JsonResource) :
public function toArray($request)
{
$defaultCover = Storage::disk('s3')->url('covers/default.jpg');
$inFavoris = $inLoans = $inWishlist = $hasCommented = false;
if(auth('api')->check()){
$inLoans = $this->loans()->where('member_id', auth('api')->id())->count() > 0 ? $this->loans()->where('member_id', auth('api')->id())->first() : false;
}
return [
'id' => $this->id,
'title' => $this->title,
'author' => $this->author ? $this->author->complete_name : false,
'resume' => $this->resume,
'cover' => $this->cover == '' ? $defaultCover : $this->cover,
'length' => $this->whenPivotLoaded('catalogs', function () {
return $this->pivot->length;
}),
'href' => $this->unique_key,
$this->mergeWhen(auth('api')->check(), [
'in_loans' => $inLoans ? true : false,
]),
'categories' => $this->categories
];
}
Is quite hard to explain it in English... Hope it'll be easier to understand what I want to achieve and where I'm stuck.
@sinnbeck