Laravel components with old() helper function

Tags: , , ,

For my easy I have made a component which contains a dropdown in it. So that I can use it everywhere when I need it. This is the component


    <select class="select" {{$required_district}} name="{{$select_district}}" id="{{$select_district}}">

        <option value="" selected> {{ __('Select the district') }} </option>

        @foreach($districts as $district)
            <option value="{{ $district->id }}"
               {{ old($select_district) && $district->id == old($select_district)  ? 'selected' : '' }}>
                {{ $district->name_en }}</option>


    <label class="text-danger" style="">{{$message}}</label>

What I do here is the title, input filed name id and other kinds of stuff make dynamically via @slot() like below


 Where do you want a job to be started




Ok things work well but when I submit the form which contains this component when the validation gets fails then it generates the below error.

array_key_exists(): The first argument should be either a string or an integer

The problem occurs with this line when form redirect back with validation errors

 {{ old($select_district) && $district->id == old($select_district)  ? 'selected' : '' }}

If I remove the {{ old($select_district) }} or if I just use it like {{ old('name_here') }} then things working as they should be. But since I need to use this component inside more pages I would like to fix this issue . So could anyone please help me to fix this.


This may be tricky but I was able to fix the error like below



Source: stackoverflow