Home » Errors » How to Fix Array to String Conversion in Laravel

How to Fix Array to String Conversion in Laravel

You can come across this exception when you try to write an array into the string variable. In most cases, there is a simple solution. Just convert the array into a string using implode(), print_r() or json_encode() methods.

In this article, we will explain how to fix the array to string conversion exception in Laravel. We will show a few different situations that may happen and the solutions for them.

1. Write Data to Model

Let’s look at an example. Imagine that you have an array:

$options = [ "option_1" => true, "option_2" => false, "option_3" => false, ];

Let’s say you want to write this array into the settings field in the Article model. When you try to do it, you will get this exception:

$article->settings = $options;

This exception is hard to solve for beginners because it points to the $model->save() method, and you don’t know where the issue is.

By default, all model fields are strings. But you may want to write an array into it. You can convert it to the string manually using json_encode() or print_r(), but there is one more convenient way. You can use casts to automatically convert a model attribute from an array to a string on saving it to the database and convert it back upon restoring data into the model.

Add the $catsts field with protected visibility to your model:

protected $casts = [ 'settings' => 'array' ];

Also, you may want to add the default value for the settings field:

protected $attributes = [ 'settings' => '[]' ];

Pay attention, that here you should specify the default value as a JSON encoded string, otherwise you will get an exception.

It is most efficient to use json or jsonb SQL field type for the settings field, but you can also use varchar if you want. In any case, after this you will be able to write and read an array from the settings field without exceptions.

2. Working with implode() method

Often you can get this exception when you try to convert an array into a string using the implode() method. For example:

$str = implode(",", $array);

First of all, you should find out what data you have. You can use var_dump(), print_r() or Symfony dd() methods to display variable contents or just use the debugger. The implode function supports only one-dimensional array. If you pass into it something like this it will not work:

$array = [ "first" => ["one", "two", "three"], "second" => ["four", "five", "six"], ];

If you want to handle this array using implode, you should use array_map() first, to make it one-dimensional. For, example, you can fetch only the first string of each array or any other logic:

implode( ", ", array_map(function ($item) { return $item[0]; }, $options) ); // one, four, seven

If you have a multi dimensional array and want to serialize it for saving, consider using json_encode() or serialize() functions instead of the explode().

Wrapping Up

In this short article we showed how to fix an array to a string conversion in Laravel-specific case and in the implode() function. When did you meet this exception? How to fix it? Tell us about it in the comments section!

Your Reaction

Leave a Comment