SilkeNL
SilkeNL

Reputation: 540

Laravel Eloquent Save doesn't save

I am trying to save survey answers in my db, because of some to me unknown reason the ->save() method is not working, ->update() is working however.

I keep getting the error Array to string conversion every time I try to save.

I have used dd/return/var_dump/print_r whatever would work, to show that it was working up to that step. So now I know it works up to the ->save() method.

My controller:

$array = json_decode($request->getContent(), true);

        foreach ($array as $survey) {
            $objAns = new Survey_Answer();
            $objAns->name = $survey['surveyName'];
            $objAns->answers = $survey['answersPerQuestion'];

            if($survey['complete'] === true) {
                $objAns['complete'] = 1;
            } else if($survey['complete'] === false) {
                $objAns->complete = 0;
            }
            $objAns->save();
        }

        return;

My model:

class Survey_Answer extends Model
     {
      protected $fillable = ['name', 'answers', 'complete'];
     } 

My migration:

public function up()
     {
        Schema::create('survey__answers', function (Blueprint $table) {
            $table->bigIncrements('id');
              $table->bigInteger('survey_id')->unsigned()->nullable()->index();
              $table->foreign('survey_id')->references('id')->on('surveys')->onDelete('cascade');
            $table->string('name');
            $table->json('answers');
            $table->boolean('complete');
            $table->timestamps();
        });
    }

I expect the code to save everything that I send along with the $request. This only results in a error: Array to string conversion.

Thank you very much for your help

Upvotes: 0

Views: 2951

Answers (2)

Remul
Remul

Reputation: 8242

I would suspect that $survey['answersPerQuestion'] is an array but you are trying to store it in a json column.

You can use Laravel's Array & JSON Casting to cast the array to a json string.

The array cast type is particularly useful when working with columns that are stored as serialized JSON. For example, if your database has a JSON or TEXT field type that contains serialized JSON, adding the array cast to that attribute will automatically deserialize the attribute to a PHP array when you access it on your Eloquent model:

Your Model:

class Survey_Answer extends Model
{
    protected $fillable = ['name', 'answers', 'complete'];

    protected $casts = [
        'answers' => 'array',
    ];
}

Once the cast is defined, you may access the options attribute and it will automatically be deserialized from JSON into a PHP array. When you set the value of the options attribute, the given array will automatically be serialized back into JSON for storage:

$user = App\User::find(1);

$options = $user->options;

$options['key'] = 'value';

$user->options = $options;

$user->save();

Upvotes: 1

user931018
user931018

Reputation: 703

You need to store your $survey['answersPerQuestion'] in json format not as an array.

$objAns->answers = json_encode($survey['answersPerQuestion']);

Although as @Lucas Arbex pointed out in the comments, there's certainly a better way to store it.

Upvotes: 1

Related Questions