0

enter image description hereI want to fetch cost of each lab_test's individually,but it gives me all the costs. I've tried using ->first(); and ->first(['cost']); in the controller but getting an error like "The Response content must be a string or object implementing __toString(), "object" given.". I'm implementing Dependent dropdown select box in my project.

Controller Test.php

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

        public function costSub(Request $request){
            $get_lab_cat_id = $request->get('labCategId');
            $lab_sub_data = \DB::table('lab_sub_category')
                ->leftJoin('lab_category','lab_category.lab_category_id','=','lab_sub_category.category_id')
                ->where('lab_category_id', $get_lab_cat_id)
                ->select('sub_category_name','lab_sub_category_id')->get();
            return $lab_sub_data;
        }

        public function costTest(Request $request){
            $get_lab_sub_id = $request->get('labSubId');
            $lab_test_data = \DB::table('lab_test')
                ->leftJoin('lab_sub_category','lab_sub_category.lab_sub_category_id'
                    ,'=','lab_test.sub_category_id')
                ->where('sub_category_id',$get_lab_sub_id)
                ->where('hide',0)
                ->select('lab_name','lab_test_id')->get();
            return $lab_test_data;
        }

        public function labTestprice(Request $request){
            $get_lab_test_id = $request->get('labtid');
            $cost = \DB::table('lab_test')
                ->leftJoin('lab_sub_category','lab_sub_category.lab_sub_category_id','=','lab_test.sub_category_id')
                ->where('lab_test_id',$get_lab_test_id)
                ->orWhere('hide',0)
                ->select('cost','lab_test_id')->get();
            //  dd($cost);
            return $cost;
        }
}

Route.php

Route::get('labdetails','Test@cost');
Route::post('get_lab_sub','Test@costSub');
Route::post('get_lab_sub_cat','Test@costTest');
Route::post('get_lab_cost_rs','Test@labTestprice');

medicinecost.blade.php

 <body>
    <h1><p>Lab Cost</p></h1><br>
    <div class="container">
        <div class="col-lg-3">
            <div class="form-group">
                <select name="labCat" id="labC" class="form-control">
                    <option value="0" disabled="true" selected="true">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>
            <div class="form-group">
                <select name="labSub" id="labS" class="form-control">
                    <option value="0" disabled="true" selected="true">Select Lab Sub Category</option>
                </select>
            </div>
            <div class="form-group">
                <select name="labTest" id="labT" class="form-control">
                    <option value="0" disabled="true" selected="true">Select Lab Test</option>
                </select>
            </div>
            <div class="col-md-2"><span id="loader"><i class="fa fa-spinner fa-3x fa-spin"></i></span></div>
        </div>

    </div>
    <div>
        <p id="testCost"></p>
    </div>
    <script>

    $(document).ready(function() {

        $('#labC').on('change', function(){
            var labCategId = $(this).val();
            if(labCategId) {
                $.ajax({
                    processing : 'true',
                    serverSide : 'true',
                    url: 'get_lab_sub',
                    type:"POST",
                    data : {labCategId:labCategId,"_token":"{{ csrf_token() }}"},
                    dataType:"json",

                    success:function(data) {
                        if(data){
                            $('#labS').empty();

                            $.each(data, function(key, value){
                                $('#labS').append('<option value="'+value.lab_sub_category_id+'">' + value.sub_category_name + '</option>');

                            });
                        }

                    },

                });
            } else {
                $('select[name="labS"]').empty();
            }

        });

        $('#labS').on('change' ,function () {
            var labSubId = $(this).val();
                if(labSubId){
                $.ajax({
                    processing : 'true',
                    serverSide : 'true',
                    url :'get_lab_sub_cat',
                    type:"POST",
                    data :{ labSubId:labSubId,"_token":"{{ csrf_token() }}"},
                    dataType: "json",
                    success:function (data) {
                        if(data){
                            $('#labT').empty();
                            $.each(data, function(key, value){

                                $('#labT').append('<option value="'+ value.sub_category_id +'">' + value.lab_name + '</option>');

                            });
                        }
                        else {
                            $('#labT').empty();
                        }
                    }
                });
            }

        })
       $('#labT').on('change' ,function () {
            var lab_test_cost_id = $(this).val();
              if(lab_test_cost_id){
                $.ajax({
                    processing : 'true',
                    serverSide : 'true',
                    url :'get_lab_cost_rs',
                    type:"POST",
                    data :{ labtid:lab_test_cost_id,"_token":"{{ csrf_token() }}"},
                    dataType: "json",
                    success:function (data) {
                        if(data){
                            //$('#labT').empty();
                            $.each(data, function(key, value){
                                $('#testCost').append('<p value="'+value.lab_test_id +'"> '+ value.cost + '</p>');
                                //$('#testCost').append('<p value="'+value.lab_test_id+'">'+value.cost+'</p>');
                            });
                        }
                    }
                });
            }
        });

    });
    </script>
    </body>

Whenever I will select lab_test from third dropdown it should the cost of that perticuler lab_tests but instead of it is showing all the costs.

4
  • exit the value $get_lab_test_id in the function abTestprice() and check what value you get, after that echo the query (using simple query statement like "select cost from tablename where your value") Commented Nov 3, 2018 at 12:38
  • lab_test_id is required to get the cost value from the table,there is dependency of 3 tables with each other.first I've to select from dropdown lab_category then lab_subcategory and the lab_test after that it will show the cost of that particular lab test Commented Nov 3, 2018 at 12:45
  • I was telling you, public function labTestprice(Request $request){ echo $get_lab_test_id = $request->get('labtid'); exit; // here what value you get. Using that value check with your normal mysql query then you know why you are getting that result Commented Nov 3, 2018 at 12:58
  • @Chrisshi I have tried with that also but still not getting the expected result.see the image Commented Nov 3, 2018 at 13:04

2 Answers 2

1

Try this:

 <body>
    <h1><p>Lab Cost</p></h1><br>
    <div class="container">
        <div class="col-lg-3">
            <div class="form-group">
                <select name="labCat" id="labC" class="form-control">
                    <option value="0" disabled="true" selected="true">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>
            <div class="form-group">
                <select name="labSub" id="labS" class="form-control">
                    <option value="0" disabled="true" selected="true">Select Lab Sub Category</option>
                </select>
            </div>
            <div class="form-group">
                <select name="labTest" id="labT" class="form-control">
                    <option value="0" disabled="true" selected="true">Select Lab Test</option>
                </select>
            </div>
            <div class="col-md-2"><span id="loader"><i class="fa fa-spinner fa-3x fa-spin"></i></span></div>
        </div>

    </div>
    <div>
        <p id="testCost"></p>
    </div>
    <script>

    $(document).ready(function() {

        $('#labC').on('change', function(){
            var labCategId = $(this).val();
            $('#labS').html('');
            $('#labS').append('<option value="0" disabled="true" selected="true">Select Lab Sub Category</option>');
            $('#labT').html('');
            $('#labT').append('<option value="0" disabled="true" selected="true">Select Lab Test</option>');
            if(labCategId) {
                $.ajax({
                    processing : 'true',
                    serverSide : 'true',
                    url: 'get_lab_sub',
                    type:"POST",
                    data : {labCategId:labCategId,"_token":"{{ csrf_token() }}"},
                    dataType:"json",

                    success:function(data) {
                        if(data){
                            $.each(data, function(key, value){
                                $('#labS').append('<option value="'+value.lab_sub_category_id+'">' + value.sub_category_name + '</option>');

                            });
                        }else {
                            $('#labS').empty();
                        }

                    },

                });
            } else {
                $('select[name="labS"]').empty();
            }

        });

        $('#labS').on('change' ,function () {
            var labSubId = $(this).val();
           $('#labT').html('');
           $('#labT').append('<option value="0" disabled="true" selected="true">Select Lab Test</option>');

            if(labSubId){
                $.ajax({
                    processing : 'true',
                    serverSide : 'true',
                    depends : ['#labC'],
                    url :'get_lab_sub_cat',
                    type:"POST",
                    data :{ labSubId:labSubId,"_token":"{{ csrf_token() }}"},
                    dataType: "json",
                    success:function (data) {
                        if(data){
                            $.each(data, function(key, value){
                                $('#labT').append('<option value="'+ value.lab_test_id +'">' + value.lab_name + '</option>');
                            });
                        }
                        else {
                            $('#labT').empty();
                        }
                    }
                });
            }

        })
       $('#labT').on('change' ,function () {
            var lab_test_cost_id = $(this).val();
            if(lab_test_cost_id){
                $.ajax({
                    processing : 'true',
                    serverSide : 'true',

                    url :'get_lab_cost_rs',
                    type:"POST",
                    data :{ labtid:lab_test_cost_id,"_token":"{{ csrf_token() }}"},
                    dataType: "json",
                    success:function (data) {
                        if(data){
                            $.each(data, function(key, value){
                                $('#testCost').append('<h5>The Lab Cost is </h5><p value="'+value.lab_test_id +'"> '+ value.cost + '</p>');
                            });
                        }
                    }
                });
            }
        });

    });
    </script>
    </body>
Sign up to request clarification or add additional context in comments.

Comments

0

Dependent Dropdown Edit

$(document).ready(function(){
$('table').on('click','#edit',function(){
var id = $(this).parents("tr").attr("id");
alert(id);

if(confirm('Are you sure to edit this record ?'))
{ 
    $.ajax({
       url: 'edit.php',
       type: 'GET',
       data: {id: id},
       error: function() {
          alert('Something is wrong');
       },
       success: function(response) {
         $('#submit').hide();
         $('#update').css('display','block');
         
        var data=JSON.parse(response);
        for(i in data){
          alert(data[i].id);
           $('#id').val(data[i].id);
           $('#name').val(data[i].name);
           $('#age').val(data[i].age);
           $('#phone').val(data[i].phone);
           $('#email').val(data[i].email);
           $('#state').val(data[i].state);
           $.ajax({
          url:'drop.php',
          type:'post',
          data:{s_id:data[i].state},
          success:function(res){
            $('#district').html(res);
        
           $('#district').val(data[i].district);
           $('#address').val(data[i].address);
           $('#gender').val(data[i].gender);

          }
         })
        }
        
    }
    });

   }

   })
   })
   </script>

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.