Galivan
Galivan

Reputation: 5338

Laravel-translatable - "call to a member function save() on string"

(Updated) I use the laravel-translatable package and trying to insert rows with translations. When trying to save, it gives me the error "call to a member function save() on string".

I loop an object with keys and values, like: "food": "Nourriture", and inside the loop I do a select of the Translations table:

$translationKey = \App\Translation::select('group', 'key')->where('group', 
'global')->where('key', $key)->first();

I don't do exactly as the documentaion, which would have been:

$translationKey = \App\Translation::where('key', $key)->first();

The difference is that I select the columns 'group' and 'key', and I do an extra "where" to specify that group = global. Isthere anything wrong there?

Then I try to check if there is an already existing translation. If not, I insert the translation:

 if($translationKey->hasTranslation('fr')) { 
     continue;
 }else{
     //insert
     $translationRow = $translationKey->translateOrNew('fr')->$key = $value;
     $translationRow->save();


 }

I use translateOrNew instead of translate , because otherwise I get error: "Creating default object from empty value".

It seems I can't do the ->save() method because it's a string, not a model instance which it should be. So I guess there is something wrong with this line?:

$translationKey = \App\Translation::select('group', 'key')->where('group', 
'global')->where('key', $key)->first();

But what is the problem?

Upvotes: 0

Views: 2539

Answers (1)

Galivan
Galivan

Reputation: 5338

I had some mistakes - I needed to select the whole row instead of individual columns:

$translationKey = \App\Translation::where('group', 'global')
->where('key', 'about_us')
->first();

And there were mistakes when saving the translation. My translations_translations table has a "value" column, so this worked:

$translationKey->translateOrNew($locale)->value = $value;
$translationKey->save()

Upvotes: 2

Related Questions