Home > Net >  pass result ajax to blade with foreach
pass result ajax to blade with foreach

Time:09-28

i am trying to pass the ajax response to my view and use it as a variable to foreach,I need it to update the quantity of bikes available on the dates selected by the user the procedure I use is: I take the selected data in the calendar, I pass them via ajax to my controller from the controller I execute the queries on the database and return to ajax a json with the data.

the ajax data must be cycled to assign the corresponding quantities to the categories but I don't know how to pass this data to the view

my view

 @foreach ($category as $cat)
  <div class="col pb-5 m-1">
      <?php 
      
      
      $categoryId=DB::table('categories')->select('id')->orderBy('id', 'asc')->get();
          foreach ($categoryId as $key) {
 
              
              $quantity[$key->id]=DB::table('bikes')->where('category_id','=',$key->id)->count('*');
          }
      ?>
      <div class="card cat m-auto position-relative " id="cat{{$cat->id}}" > 
          @foreach ($qty as $item=>$val)
          @if ($item==$cat->id) 
          <p class="m-3" id="numberqty" style="font-weight: bold;color:#ce2e30;text-shadow: 2px 5px 3px rgba(150, 150, 150, 0.92);">{{-- x{{$val}} --}}</p> 
          @endif
          @endforeach
          <p class="check position-absolute" style="display: none"><i class="fa fa-check" aria-hidden="true"></i></p>

          <input class="cat-id" type="number" value="{{$cat->id}}" hidden>
          <input class="id-cat" type="checkbox" name="category[]" id="category" value="{{$cat->id}}" hidden>
          <img class="card-img-top p-3" src="{{asset('storage/'.$cat->cover_image)}}" alt="">
          <div class="card-body my-n3">
              <h3 class="card-title text-center" style="text-shadow: 2px 5px 3px rgba(150, 150, 150, 0.92);">{{$cat->tipo}}</h3>


                      
                  {{-- <h3 class="card-title">Disponibilità: {{$val}}</h3> --}}
                      
                          <button class="btn btn-primary pl-5 pr-5 ml-5 drop"  type="button">{{__('payment.page.price')}}</button>

                          <button class="btn btn-primary pl-5 pr-5"  type="button">Dettagli</button>

              
              

              <div class="show-drop" style="display: none">
                  <h3 class="text-center my-1">{{__('payment.page.price')}}</h3>
                  
                  <p class="text-center">    {{__('payment.page.1day')}} {{$cat->base}} </p> <hr>
                  <p class="text-center">     {{__('payment.page.2day')}} {{$cat->twoDay}}</p> <hr>
                  <p class="text-center">     {{__('payment.page.3day')}} {{$cat->threeDay}}</p> <hr>
                  <p class="text-center">    {{__('payment.page.4day')}} {{$cat->fourDay}}</p> <hr>
                  <p class="text-center">   {{__('payment.page.5day')}} {{$cat->fiveDay}}</p> <hr>
                  <p class="text-center">     {{__('payment.page.6day')}} {{$cat->sixDay}}</p> <hr>
                  <p class="text-center">   {{__('payment.page.7day')}} {{$cat->sevenDay}}</p>
                  <p class="text-center">   {{$cat->overprice}} {{__('payment.page.overprice')}}</p>
                  {{-- <p>{{$cat->overprice}}</p> --}}

              </div>

              
          </div>

      </div>
      <div class="number-drop" style="display: none">
          <label for="quantity{{$cat->id}}">Nome</label>
          <input name="{{$cat->id}}" type="number" id="quantity{{$cat->id}}" class="form-control" value="0" >
      </div>
  </div>
  @endforeach

my ajax

    $('#range_date').on('change', function(){
        
        var data = $(this).val();
        var split = data.split(' - ');
        var startDate = split[0];

        var dayStart = startDate.substr(0,2);
        var monthStart = startDate.substr(3,2);
        var yearStart = startDate.substr(6,4);

        var startCorrect = monthStart "/" dayStart "/" yearStart;
        console.log(startCorrect);



        var endDate = split[1];
        var dayEnd = endDate.substr(0,2);
        var monthEnd = endDate.substr(3,2);
        var yearEnd = endDate.substr(6,4);

        var endCorrect = monthEnd "/" dayEnd "/" yearEnd;
        console.log(endCorrect);


        var  date1 = new Date(startCorrect);
        var  date2 = new Date(endCorrect);
        var  diffTime = Math.abs(date2 - date1);
        var  diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 


        $.ajax({
            
            "url": "bookingCheck",
            "data": {
                "start": startCorrect,
                "end":endCorrect
                
            },
            "method": "GET",
            success: function (response) {
                let res = response;
                let qty = res['qty'];

                console.log(qty);
                
               document.getElementById('numberqty').innerHTML=qty;
                $('#numberqty').html(qty.html);

            }
        });
    });

my controller

    public function checkBike(Request $request){
        $date1=Carbon::parse($request->start)->format('Y-m-d');
        $date2=Carbon::parse($request->end)->format('Y-m-d');
        

        $contract=DB::table('contracts')->get();
        $contractdate=DB::table('contracts')->whereRaw('? between data_inizio and data_fine', [$date1,$date2])->get();

        if (count($contractdate) > 0) {

            foreach ($contractdate as $key) {
                $id=$key->id;
            }
            $bikeContract=DB::table('bike_contract')->where('contract_id','=',$id)->get();
    
            foreach ($bikeContract as $key) {
                $bikeselect[$key->id]=DB::table('bikes')->where('id','=',$key->bike_id)->get();
                
            }
    
            foreach ($bikeselect[$key->id] as $key) {
                $idcat=$key->category_id;
                $quantity=DB::table('bikes')->select('category_id')->whereNotIn('category_id',[$idcat])->get();
            }
            foreach ($quantity as $key) {
                $qty[$key->category_id]=DB::table('bikes')->where('category_id','=',$key->category_id)->count('*');
            }
            return response()->json(["qty"=>$qty]);
        } else {
            $categoryId=DB::table('categories')->select('id')->orderBy('id', 'asc')->get();
                foreach ($categoryId as $key) {
            
                    $quantity[$key->id]=DB::table('bikes')->where('category_id','=',$key->id)->count('*');
                }
        return response()->json(["qty"=>$quantity]);
        }
    }
}

CodePudding user response:

Try this:

success: function (response) {
  response = JSON.parse(response);
  let qty = response.qty;
}
  • Related