Wednesday, July 15, 2020

Show More/Vew More Button in Laravel & Ajax in single Page ?

blade.php

<div id="load_data">
</div>

<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 my-4">
                    <center>
                        <button id="loadMoreData" data-pos="{{ $take }}" class="btn btn-primary view_btn_second">Show More</button>
                    </center>
                </div>


Js:

script type="text/javascript">

     $('#loadMoreData').on('click', function(){

        var pos=$(this).data('pos');
        var offer_id="{{ $offer->id }}";

        //$("#loadMoreData").html("Loading....");

        $.ajax({

            url:'{{ route("loadmore.load_data") }}',
            method : "GET",
            data : {pos: pos, offer_id: offer_id},

             success:function(res){

                //console.log(res);

                res.products.forEach(function(row){

                    var html='';
                    html+="<div class='col-6 col-md-4 col-lg-3 col-xl-3 col'>";
                    html+="<div class='product fixed_hight'>";

                    html+="<figure class='product-image-container image-frame'>";
                    html+="<a class='product-image' href='"+row.product_url+"'>";
                    html+="<img class='product_img_tag' src='"+row.product_image_url+"' alt='"+row.name+"'/>";
                    html+="</a>";
                    html+="<a href='"+row.quick_view_url+"' class='btn-quickview'>Quick View</a>";
                    html+="</figure>";

                    html+="<div class='product-details'>";

                    html+="<h1 class='text-left product-category'><a href='"+row.brand_url+"'>"+row.brand_name+"</a></h1>";


                    html+="<h1 class='product-title text-left'><a href='"+row.product_url+"'>"+row.brand_product_title+"</a></h1>";

                    html+="<span><div class='price-box float-left'>";

                    if(row.old_price_status){
                      html+="<span class='old-price'>৳ "+row.price_tax_inc+"</span>";
                    }

                    html+="<span class='product-price'>৳ "+row.price+"</span>";

                    html+="</div></span>";

                    html+="</div><br>";
                    //End of product detail

                    if(row.product_in){

                      html+="<span style='position: absolute;bottom: 0px;right: 28%;' class='paction add-cart cursor_pointer addToCart' data-link='"+row.add_to_cart_url+"' data-product_id='"+row.id+"'  data-product_name='"+row.name+"' data-old_price='"+row.price_tax_inc+"' data-product_price='"+row.price+"' data-product_slug='"+row.slug+"' data-product_image='"+row.thumbnail_image+"' title='Add to Cart'><span>Add to Cart</span></span>";

                    }else{

                      html+="<span style='position: absolute;bottom: 0px;right: 28%;color: #c1b6b6' class='paction add-cart cursor_pointer' title='Out of stock'><span>Out of Stock</span></span>";

                    }

                    html+="</div>";
                    html+="</div>";

                    $('#load_data').append(html);

                });

                if(res.next_pos) $('#loadMoreData').data('pos', res.next_pos);
                else $('#loadMoreData').hide();

                $('.product_img_tag').on('error', function(){
                  //$(this).src=res.default_img;
                  $(this).attr("src", res.default_img);
                });

</script>

web.php
Route::get('/loadmore/load_data', 'Website\HomeController@load_data')->name('loadmore.load_data');

Controller:

public function load_data(Request $request){

    $pos=$request->pos;
    $offer_id=$request->offer_id;

    $offer_image = Offer::where('id', $offer_id)->first();
    $offer =$offer_image;
    $offer_cat_ids = OfferCategory::where('offer_id', $offer_id)->pluck('category_id')->toArray();

    $offer_cats =Category::whereIn('id',$offer_cat_ids)->get();

    $offer_children = Offer::where('parent_id',$offer_id)->get();

    $cat_product_ids =ProductCategory::where('category_id',$offer_cat_ids)->pluck('product_id')->toArray();

    $offer_product_ids=[];

    if($offer->offerDetails){
       $offer_product_ids = $offer->offerDetails->whereIn('product_id',$cat_product_ids)->pluck('product_id')->toArray();
    }
   
    $take = 4;

    $products = Product::with([
      'category','thumbnail'
    ])->whereIn(
      'id', $offer_product_ids
    )->where([
      'status'=>1,
      'available'=>1,
      'product_type'=>'step_one'
    ])->orderBy('id','DESC')->skip($pos)->take($take)->get();

    $produsts_data=[];
    $carbon = new \Carbon\Carbon;

    foreach($products as $product){

      $price = 0;
      $old_price_status = 0;
   
      if($product->offer()->exists() && $product->offer->status==1 && $product->offer->end_date !=null) {
          if ($carbon->now()->diffInSeconds($carbon->parse($product->offer->end_date), false) >= 0) {
              $price = $product->price_tax_inc - $product->discount_amount;
              if ($product->discount_amount >0 ) {
              $old_price_status =1;                         
              }                     
          }else{
              $price = $product->price_tax_inc;
          }                     
      }else{
          $price = $product->price_tax_inc;
      }

      array_push($produsts_data, [
        'id'=>$product->id,
        'name'=>$product->name,
        'price'=>$price,
        'slug'=>$product->slug,
        'product_url'=>url('product', $product->slug),
        'product_image_url'=>asset("backoffice/images/products/regular/".cn($product, 'thumbnail.image', '#')),
        'quick_view_url'=>url('product/quick_view', $product->slug),
        'brand_url'=>route('home.brand.products', ['brand'=>cn($product, 'brand.slug', '#')]),
        'brand_name'=>cn($product, 'brand.name', NULL),
        'brand_product_title'=>str_limit($product->name, 50),
        'old_price_status'=>$old_price_status,
        'price_tax_inc'=>$product->price_tax_inc,
        'product_in'=>$product->product_in,
        'add_to_cart_url'=>url('add-to-cart', $product->id),
        'thumbnail_image'=>cn($product, 'thumbnail.image', '#')
      ]);

    }

    if($take > $products->count()){
      $next_pos=NULL;
    }else{
      $next_pos=$pos+$take;
    }

    return [
      'next_pos'=>$next_pos,
      'products'=>$produsts_data,
      'default_img'=>asset('website/assets/images/default.jpg'),
    ];

  }

No comments:

Post a Comment

Ajax load lage with laravel.

 step-1:  HTML <div class="row">                     <div class="col-lg-12">                           <d...