0

I'm trying to running my coding but I got error which is undefined variable.

Here's my controller:

$usr_color = DB::table('listbox')
                ->select('lstbx_codename','lstbx_descname')
                ->where('lstbx_cat', '=', 'usr_prf_color')
                ->orderBy('lstbx_no', 'asc')
                ->get();

Here's my view:

                                        <div class="form-group">
                                            <label>Color</label>
                                            <select name="usr_prf_color" class="form-control">
                                                @foreach ($usr_color as $color) 
                                                    <option value="{{ $color-> lstbx_codename }}" 
                                                        {{ old('usr_prf_color') == $color-> lstbx_codename ? 'selected' : '' }}>
                                                        {{ $color-> lstbx_descname }}
                                                    </option>
                                                @endforeach
    
                                                
                                                --}}
    
                                             </select>
                                        </div> 

My error is "Undefined variable: usr_color".

4
  • 1
    add the specific error and how you are returning the variable from controller to your question. Commented Nov 25, 2020 at 4:23
  • are you sure your passing $usr_color to you view ? put that code Commented Nov 25, 2020 at 4:25
  • Are you returning $usr_color in the controller's response? Commented Nov 25, 2020 at 4:25
  • yes, I'm returning the $usr_color in the controller also. Commented Nov 25, 2020 at 4:28

1 Answer 1

3

You can check that variable before loop

@if(!empty($usr_color))

   // you can loop here

@endif
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.