question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getTranslations() return string and expecting array

See original GitHub issue

Hi there again @tabacitu

So as we spoke on #492 the problem really was MariaDB. I had to remove MariaDB and go with MySQL.

After that i run into some strange problem, so i will describe it to you.

This is my model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
use Backpack\CRUD\ModelTraits\SpatieTranslatable\Sluggable;
use Backpack\CRUD\ModelTraits\SpatieTranslatable\SluggableScopeHelpers;
use Backpack\CRUD\ModelTraits\SpatieTranslatable\HasTranslations;



class Company extends Model
{
    use CrudTrait;
    use Sluggable, SluggableScopeHelpers;
    use HasTranslations;

protected $translatable = ['name'];

When i create a new resource it get save into database. The name becomes: {"en": "Bruno Tavares"} And the slug: {"en": "{\"en\":\"bruno-tavares\"}"}

I think the slug is beeing generated from {"en": "Bruno Tavares"} and not from "Bruno Tavares"

After saving to the database it returns to the list of companys, and that’s when i get the following error:

ErrorException in HasTranslations.php line 72:
Type error: Return value of App\Models\Company::getTranslations() must be of the type array, string returned (View: C:\wamp\www\megaboff\resources\views\vendor\backpack\crud\columns\text.blade.php) (View: C:\wamp\www\megaboff\resources\views\vendor\backpack\crud\columns\text.blade.php)

My fields on database are type JSON as i’v updated also doctrine/dbal to work with them within laravel.

Any idea why this could be happening?

EDIT PLEASE CONSIDER THE FOLLOWING:

When i use my seeder to populate the database with fake data i got the same formating in database as if i create a new model instance using the create crud. The error only happen if i have data created with seeder, if i delete the data created with seeder the error goes away.

What i did to test this ?

I created some companys using the seeder, and created another exactly the same but using the crud panel. (only id is diff) (btw, the sluggable shoudn’t ensure uniqueness?) After i deleted the ones generated by my seeders the error goes away, but i’m left with the same info in the database for the row i did not deleted as i had from the previous created with the seeder.

This is my factory:

$factory->define(App\Models\Company::class, function (Faker\Generator $faker) {
    static $domain;
    $tID = \App\Models\Template::inRandomOrder()->first()->id;
    $name = $faker->name;
    //$slug = str_slug($name);
    return [
        'name' => ['en' => $name],
        'address' => $faker->address,
        'email' => $faker->unique()->safeEmail,
        'phone' => $faker->numerify('#########'),
        'nif' => $faker->numerify('#########'),
        'active' => $faker->boolean($chanceOfGettingTrue = 80),
        'logo' => $faker->imageUrl($width = 640, $height = 480),
        'default_lang' => $faker->languageCode,
        'domain' => $domain ?: $domain = "www.backmotivos.dev",
        'template_id' => $tID,
        'extras' => json_encode($faker->creditCardDetails),
    ];
});

You can check this image: http://prntscr.com/ebnjr9

When i left only id 4 (the one i created using crud panel) it simply works …

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
lloy0076commented, Aug 12, 2017

Is this issue still happening? I think this is a question rather than a bug but it could be a bug…

0reactions
tabacitucommented, Sep 4, 2017

PS. I closed the issue as it’s been a while and nobody has reported on it. Feel free to reopen it if it’s still a bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

laravel placeholder translation of an array - php - Stack Overflow
ERROR: strtr() expects parameter 1 to be string, array given Can laravel https://laravel.com/docs/9.x/localization#replacing-parameters-in- ...
Read more >
Call to a member function getTranslation() on null in ... - Drupal
It seems that it cannot get the original entity and call the function getTranslation() on it. So, possibly, a check for the existence...
Read more >
How To Use ngx-translate with Angular - DigitalOcean
Creating the translation loader in this way expects you to have a file in your project ... get(key: string|Array<string>, interpolateParams?
Read more >
Translate - Azure Cognitive Services - Microsoft Learn
This parameter is used to get translations from a customized ... Each array element is a JSON object with a string property named...
Read more >
Working with String Enums in TypeScript | HTML Goodies
In truth, string enums are not as easily iterated over as Arrays, ... The AppComponent's getTranslation() method merely delegates the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found