0

I am using this package for managing translations in a Laravel project. The resulting output of an API endpoint is as shown in the image with translation array.

In there documentation (link: https://docs.astrotomic.info/laravel-translatable/package/methods#static-disableautoloadtranslations) it is mentioned we can disable this translation array on entire project from config file or disable per model using this function static disableAutoloadTranslations(). I am not sure how to call this on a per model basis?

enter image description here

    namespace App\Models;
    use Astrotomic\Translatable\Translatable;
    use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;

    class Category extends Model{
    
    use Translatable, SoftDeletes;
    public $translatedAttributes = ['title'];

    protected $guarded = ['id'];

    public $guard_name = 'admin';

    public function getIconAttribute($value)
    {
        return ($value != null) ? Storage::disk('public')->url($value) : null;
    }

    self::disableAutoloadTranslations();

}
abhiklpm
  • 1,603
  • 2
  • 16
  • 21
  • Can you show your Model code? If you are using Translatable trait in your Model (use \Dimsav\Translatable\Translatable) , you just need to write: 'self::disableAutoloadTranslations()' – mmarques Jun 02 '21 at 12:43
  • @mmarques I imported the Trait, but I am not sure how to add that in model, when I put in model self::disableAutoloadTranslations(); "message": "syntax error, unexpected 'self' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)", – abhiklpm Jun 02 '21 at 14:40
  • Please show your Model code so we can try to help you. – mmarques Jun 02 '21 at 15:01
  • @mmarques updated question with Model code – abhiklpm Jun 02 '21 at 15:34
  • could you add your controller method that calls this data? – Joseph Jun 02 '21 at 15:39
  • @mmarques return Category::disableAutoloadTranslations()->get();, when I write like this it returns error (Call to a member function get() on null), but Category::get() returns result with translation array. – abhiklpm Jun 02 '21 at 15:43
  • @mmarques got another work around which solves the problem, disabled translations by default from config, then loaded in controller using eagerloading like : return Category::with('translations')->get(); This solves the problem but not sure how to do it the other way. – abhiklpm Jun 02 '21 at 15:59
  • @mmarques same issue is mentioned here also: https://github.com/dimsav/laravel-translatable/pull/484 but when I call Post::disableAutoloadTranslations() it returns null. – abhiklpm Jun 02 '21 at 16:01
  • @abhiklpm the disableAutoloadTranslations returns void as you can see here: https://github.com/dimsav/laravel-translatable/blob/79ca742f7c2189770faacfa655ca4f71851f8f1e/src/Translatable/Translatable.php#L573 – mmarques Jun 07 '21 at 10:33
  • 1
    @abhiklpm and in your model you should call your self::disableAutoloadTranslations(); inside the Model boot. Example: public static function boot() { parent::boot(); self::disableAutoloadTranslations(); } – mmarques Jun 07 '21 at 10:35
  • @mmarques thankyou, I will try – abhiklpm Jun 12 '21 at 15:30

0 Answers0