Code Monkey home page Code Monkey logo

Comments (9)

dimsav avatar dimsav commented on June 9, 2024

Have you tried adding with('translations') before converting to array? It will have the same effect.

Example: Country::with('translations')->find(1)

from laravel-translatable.

jcwatson11 avatar jcwatson11 commented on June 9, 2024

The proposed solution has no different effect.

I think I must not have communicated the problem well.

The problem is that when serializing an Eloquent Model that uses Translatable with the Model::toJson() method call, the translations are not used because the underlying Eloquent model retrieves attributes of the fact data table instead of the translated attribute found in the Translation table.

For example:

Let's say we have a widget table with a title:

CREATE TABLE `Widget` (
  `WidgetId` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`WidgetId`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Now let's create the translation table for it:

CREATE TABLE `WidgetTranslation` (
  `WidgetTranslationId` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `WidgetId` int(10) unsigned NOT NULL,
  `Title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `Locale` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`WidgetTranslationId`),
  UNIQUE KEY `widgettranslation_widgetid_locale_unique` (`WidgetId`,`Locale`),
  KEY `widgettranslation_locale_index` (`Locale`),
  CONSTRAINT `widgettranslation_widgetid_foreign` FOREIGN KEY (`WidgetId`) REFERENCES `Widget` (`WidgetId`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Now let's create the model:

class Widget extends \Eloquent
{
    use \Dimsav\Translatable\Translatable;
    public $translatedAttributes = array('Title');
    public $translationForeignKey = 'WidgetId';
    public $translationModel = 'WidgetTranslation';
    public $localeKey = 'Locale';
    protected $table = 'Widget';
    protected $primaryKey = 'WidgetId';
    protected $fillable = ['Title'];
    public $timestamps = false;
}

And finally the translation model:

class ReferenceListTranslation extends \Eloquent
{
    protected $table = 'WidgetTranslation';
    protected $primaryKey = 'WidgetTranslationId';
    protected $fillable = ['Title'];
    public $timestamps = false;
}

Now, let's add a record with a translation:

<?php
$widget = new Widget();
$widget->translate()->Title = "This is my widget";
$widget->save();

Now let's retrieve that widget in an array and serialize it:

<?php
$widgets = Widget::get();
echo $widgets->toJson();

Actual Output:

[{"WidgetId":1,"Title":null}]

Expected Output:

[{"WidgetId":1,"Title":"This is my widget"}]

In any case, thanks for your work. This is a great project, and I'm really glad for your contribution!

from laravel-translatable.

domifromch avatar domifromch commented on June 9, 2024

I would appreciate to have the toArray() method override added to the trait as @jcwatson11 suggested. This would help using the toJson() method and wouldn't need any getter-method. Thumbs up.

from laravel-translatable.

dimsav avatar dimsav commented on June 9, 2024

Guys I would appreciate if you make a PR for this + tests. My time is limited at the moment.

Thanks

from laravel-translatable.

dimsav avatar dimsav commented on June 9, 2024

Done. Coming soon for version 5.

from laravel-translatable.

EcoFreak avatar EcoFreak commented on June 9, 2024

Why is didin't you include this in version 4 for Laravel 4?

from laravel-translatable.

dimsav avatar dimsav commented on June 9, 2024

@EcoFreak it is already implemented in v4.4 :)

from laravel-translatable.

EcoFreak avatar EcoFreak commented on June 9, 2024

I had the composer requirement set to an earlier version. I've tried it and it returns me the object with an array attribute named "translations" instead of including only the values of the current language.Can you help?

from laravel-translatable.

dimsav avatar dimsav commented on June 9, 2024

Please check the tests and the readme file to see how to implement.

from laravel-translatable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.