speedydan
8 months ago

Form - show old array data and existing model data

Posted 8 months ago by speedydan

Hey,

I have a form I'm using for editing a listing.

It includes a section allowing a user to add multiple highlights. This is just a text field that duplicates each time a user clicks 'add another highlight'.

My question is - how do I show the old data for these fields, along with any features a user has already added.

so far I have this:

// Show existing highlights
@if($listing->highlights->count())
    @foreach($listing->highlights as $highlight)
         <input type="text" placeholder="e.g. One-to-one tutorials" name="highlights[]" class="highlight form__input mb-3" value="{{ $highlight->highlight }}">
    @endforeach
@endif

@if(is_array(old('highlights')))
    @foreach(old('highlights') as $highlight)
        <input type="text" placeholder="e.g. One-to-one tutorials" name="highlights[]" class="highlight form__input mb-3" value="{{ $highlight }}">
    @endforeach
@endif

This mostly works - but as I also need to show old data (in case there are any validation errors) - the old data is just duplicating the existing features.

can anyone point me in the right direction?

Thanks! :-)

Please sign in or create an account to participate in this conversation.