Skip to content
Advertisement

ErrorException Array to string conversion in Laravel form submission with repeater fields

In my Laravel application, I have the following form to submit data:

JavaScript

This is my create.blade.php

And my controller looks like this:

JavaScript

Now the problem is, whenever I tried to submit the data, it gives me an error saying

ErrorException Array to string conversion

test_type and test_result fields are repeaters. Due to that, I’ve used those field names as test_type[] and test_result[].

Advertisement

Answer

As you’re trying to save an array in your database, you need to define a text or json column in your database and cast the field to one of array, json or collection types.

Define cast in TestResult model:

JavaScript

Learn more about casting in laravel models from here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement