0

i'm trying to fetch data from the database in dropdown list but it is not showing ,i have also watch several tutorials but things are not working as expected

Controller :

 public function cost(Request $request){
            $lab_data = \DB::table('lab_category')->select('category_name')->get();
              //return $lab_data;
        return view('medicinecost')->with('lab_category',$lab_data);
          }

Route :

Route::get('labdetails','Test@cost');
Route::get('labprice',function (){
    return view('pages/medicinecost');
});

medicinecost.blade.php

<div class="form-group">
    <select name="labCat" id="lab" class="form-control input-lg dynamic" data-dependent="labSubCat">
    <option value="{{$lab_data}}">Select Lab Category</option>
        @foreach($lab_data as $lb)
            <option value="{{$lb->lab_category_id}}">{{$lb->category_name}}</option>
        @endforeach
    </select>
</div>

I am getting this error

ErrorException in 1180188cd7aaaebdb54a13bbf08bc2d80ab30f15.php line 15:
Undefined variable: lab_data (View: C:\xampp72\htdocs\hospital\resources\views\pages\medicinecost.blade.php)
8
  • show your medicinecost file Commented Oct 31, 2018 at 5:48
  • can you provide the code of your view where you are trying to populate dropdown Commented Oct 31, 2018 at 5:48
  • use lab_category instead of lab_data in foreach loop Commented Oct 31, 2018 at 5:51
  • i have tried it @BhargavChudasama but giving me the same error. Commented Oct 31, 2018 at 5:57
  • 1
    you'll surely get an error/bug if the problem (here) is solved because you select only category_name and you try to use lab_category_id also. Change your select conditions Commented Oct 31, 2018 at 6:38

5 Answers 5

3

use this

<div class="form-group">
    <select name="labCat" id="lab" class="form-control input-lg dynamic" data-dependent="labSubCat">
    <option value="">Select Lab Category</option>
        @if(isset($lab_category))
        @foreach($lab_category as $lb)
            <option value="{{$lb->lab_category_id}}">{{$lb->category_name}}</option>
        @endforeach
        @endif
    </select>
</div>


Route::get('labprice',function (){
    $lab_data = \DB::table('lab_category')->select('category_name')->get();
          //return $lab_data;
    return view('pages/medicinecost')->with('lab_category',$lab_data);
});
Sign up to request clarification or add additional context in comments.

6 Comments

i have used this your code @Shaielndra Gupta ,undefined variable solved but list is not coming
@Prathamesh as I previously said you have to clarify on which route you are getting the error
On Route labprice i'm getting an error,your code solves 80% problem,i'm almost there
on that route, you are directly returning the view so how that variable can be accessed
i have used this route but it gives an error View [medicinecost] not found.
|
2

In Controller you need to select lab_category_id as well :

$lab_data = \DB::table('lab_category')->select('lab_category_id','category_name')->get();

Also fix the way you pass params, And no need to pass lab_category :

return view('medicinecost')->with('lab_data');

In view make its default value like 0 or empty:

<option value="0">Select Lab Category</option>

Comments

2

try this code

controller

 return view('medicinecost',['lab_category'=>$lab_data]);

medicinecost.blade.php

<div class="form-group">
    <select name="labCat" id="lab" class="form-control input-lg dynamic" data-dependent="labSubCat">
    <option value="">Select Lab Category</option>
        @foreach($lab_category as $lb)
            <option value="{{$lb->lab_category_id}}">{{$lb->category_name}}</option>
        @endforeach
    </select>
</div>

Comments

1

Error is here

<option value="{{$lab_data}}">Select Lab Category</option>

and also here

@foreach($lab_data as $lb)

You're defining $lab_data in the controller, but passed with the variable of lab_category in the view

So, used @foreach($lab_category as $lb) as they mentioned before in the answer and remove {{ $lab_data }} in the option-value

3 Comments

yes tried the same but still data is not populating in the dropdown
yes man i have used dd(); for that variable and it is coming in JSON format
json format? can you quickly change your return view like this return::view('medicinecost', ['lab_category' => $lab_data ]); but not sure whether it will solve your problem or not
1

You may try this.

Your Controller

public function cost(Request $request){
            $lab_category = \DB::table('lab_category')->select('category_name','lab_category_id')->get();
        return view('medicinecost', compact('lab_category'));
          }

Your View

<div class="form-group">
    <select name="labCat" id="lab" class="form-control input-lg dynamic" data-dependent="labSubCat">
    <option value="">Select Lab Category</option>
        @foreach($lab_category as $lb)
            <option value="{{$lb->lab_category_id}}">{{$lb->category_name}}</option>
        @endforeach
    </select>
</div>

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.