Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jayejay's avatar

Laravel getting an “Array to string conversion” while storing an Array into a Json database field

I'm trying to save an array with options into a json datafield of my postgres database. I'm working with Laravel 5.5 and I'm using the extension "dimsav/laravel-translatable" for translations.

My model Question looks like this: namespace App;

use Illuminate\Database\Eloquent\Model;
use Dimsav\Translatable\Translatable;

class Question extends Model
{
    use Translatable;
    public $translatedAttributes = ['options', 'answer'];

    protected $casts = [
        'options' => 'array'
    ];
}

The model QuestionTranslation looks like this:

namespace App;

use Illuminate\Database\Eloquent\Model;

class QuestionTranslation extends Model
{

public $timestamps = false;
public $fillable = ['options', 'answer'];

}

And the store action in the QuestionsController:

 public function store(Request $request)
{
    $question = new Question();

    $options[1] = "test1";
    $options[2] = "test2";

    $question->answer = 1;
    $question->options = $options;

    $question->save();

}

When I try to store that data I get the error:

Illuminate \ Database \ QueryException
Array to string conversion (SQL: insert into "question_translations" ("locale", "answer", "options", "question_id") values (en, 1, test1, 18) returning "id")

When I use json_encode to cast $options myself, I can store it without problems.

Do you have any Idea, why the laravel casting is not working? Maybe because of the translatable extension?

Thanks for helping me :-).

0 likes
8 replies
GertjanRoke's avatar

Did you try to put the $casts variable on your translation model?

1 like
GertjanRoke's avatar
Level 4

@jayejay what if you use a setOptionsAttribute method on your translation model?

something like this:

public function setOptionsAttribute($options)
{
    $this->attributes['options'] = json_encode($options);
}

You could give that a try and I will look in my projects how I fixed it.

1 like
jayejay's avatar

@GertjanRoke

Setting this method helps. My options array will be saved to the database now. Thank you! :-)

But I'm still wondering, why the $casts way is not working.

1 like
GertjanRoke's avatar

@jayejay nice to hear that it works, which version are your using of Laravel? Maybe the documentation says something about it.

GertjanRoke's avatar

@jayejay I think this is a bug in the Dimsav package, I was looking in through some of his code and found where the values are set.

if ($this->isTranslationAttribute($attribute)) {
    $this->getTranslationOrNew($locale)->$attribute = $value;
} else {
    return parent::setAttribute($key, $value);
}

I thing the problem is that he does not use the setAttribute method within the if part of the statement, because the setAttribute method checks if the attribute needs to be cast or not.

Falilou's avatar

This is my error Array to string conversion (SQL: insert into users_details (user_id, dob, gender, height, marital_status, body_type, complexion, country, city, state, education, occupation, income, about_myself, about_partner, hobbies, languages, updated_at, created_at) values (5, 10/10/1992, Homme, 5' 5', Unmarried, Slim, Fair, Guinée, Basse Guinée, Conakry, Licence, Biologist/Botanist, $75,001-100,000, ojrslkj, kljaewf;lkhcjn, Activités extérieurs, Aquariums, , Abkhazian, Afar, , 2019-01-12 13:48:40, 2019-01-12 13:48:40))

Please or to participate in this conversation.