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

DanNeon's avatar

How to get old value with array in select input

I have a select element that looks like this

<select class="form-control" id="authorSelect1" name="author[1][author]" required>
    <option value=""> -- </option>
         @foreach ($authors as $author)
            <option value="{{ $author->id }}" {{in_array($author->id, old("author") ?: []) ? "selected": ""}}>
            {{ $author->name }}
        </option>
         @endforeach
</select>

with the option to add more dropdowns via js.

where my controller then looks like this

foreach ($request->author as $data) {
            $newPost->authors()->attach($data['author']);
        }

Laravel validation is working great with the rest of the form, but I can't work out how to get the old values back to each select if the form fails.

Can anyone help?

0 likes
4 replies
Sinnbeck's avatar

Isn't the author always a index 1?

<option value="{{ $author->id }}" {{$author->I'd == old("author")[1]['author'] ? "selected": ""}}>
DanNeon's avatar

Thanks Pio, that returns the last item in the list as selected each time.

DanNeon's avatar

Thanks @sinnbeck , that's working well. I had tried something a little like that, but not with your

old("author")[1]['author']

syntax. Thank you!

Please or to participate in this conversation.