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

ziggyspider's avatar

"ErrorException Array to string conversion" on CREATE form Validation (checkbox array)

I think this is issue is related with validation,

I have a STORE REQUEST on request folder to the validation with this rule

public function rules()
{
    return [
        'zones' => 'required|array|min:1'
    ];
}

I have one the controller for the CRUD, on the create method:

public function create()
{
    return view('labels.create', ['countries' => Country::orderBy('name')->pluck('name', 'id')]);
}

I have a create view file with a form:

@foreach($countries as $id => $name)
<div>
    <label>
        <input type="checkbox" name="zones[]" value="{{ $id }}">
        {{ $name }}
    </label>
</div>
@endforeach

I got this error http://flareapp.io/share/W7zjpbl7

if I remove this rule 'zones' => 'required|array|min:1' everything works fine, the form save without problems, I need that checkboxes as required.

Any help? thank you

0 likes
8 replies
MichalOravec's avatar
Level 75

zones is an array, you can't save it in Label.

$label = Label::create($request->except('zones'));
1 like
jlrdw's avatar

Yes it's an array, you loop over array to see what's checked, a no checked doesn't pass anything in HTTP.

You have to handle the ones not checked.

1 like
ziggyspider's avatar

I forgot show my store method:

    public function store(StoreLabelRequest $request)
    {
        $label = Label::create($request->validated());

        foreach ($request->zones as $zone) {
            $label->zones()->create([
                'country_id' => $zone
            ]);
        }

        return redirect()->route('admin.labels.index');
    }
MichalOravec's avatar

It's irrelevant. zones is an array and you can't save it as a field.

ziggyspider's avatar

So I need save it as an array, if yes, how I can save it as an array?

Also why works if I remove this rule:

'zones' => 'required|array|min:1' 

?

MichalOravec's avatar

$request->validated() return what you have in validation rules...

So when you have there

'zones' => 'required|array|min:1' 

Then $request->validated() return

[
    'zones' => [1,2 3, 4] // just an example
]

Which means zones is an array and I don't think that you have zones column in labels table...

You use zones for zones relationship.

I really don't know how labels table looks like.

And I already told you what you have to do...

$label = Label::create($request->except('zones'));
1 like
ziggyspider's avatar

thank you! I got it now! and you are correct.

and yes, with the except works.

yes labels table don't have the zones column, I use another table called label-zones to the relationship. https://imgur.com/0iUDA7k

Please or to participate in this conversation.