Astrotomic / laravel-translatable

A Laravel package for multilingual models

Home Page:https://docs.astrotomic.info/laravel-translatable/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`translations` relationship is fetched even if it's not eager loaded.

MohammedElattar opened this issue · comments

  • in ProductController, I eager load translation relationship, but when I use whenLoaded in a resource with specifying a
    default value for translation, it causes N+1 queries.

          'translation' => $this->whenLoaded(
              'translation',
              new ProductTranslationResource($this->translation)
          ),
          'translations' => $this->whenLoaded(
              'translations',
              ProductTranslationResource::collection($this->translations)
          ),
    
  • but it works if I don't pass a default value in whenLoaded method or using the following code:

      if($this->relationLoaded('translation')){
          $resource['translation'] =  new ProductTranslationResource($this->translation);
      }
    
      if($this->relationLoaded('translations')){
          $resource['translations'] = ProductTranslationResource::collection($this->translations);
      }
    

Great Work. ❤

This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days