mardi 7 juillet 2020

Products is undefined Laravel

I'm getting this error Undefined variable: $productsTR while I have set the products variable in controller, I have a main blade and a partial blade. Main blade is the one that sends data to controller then returns to partial blade. How can I fix this Undefined variable: $productsTR?

HomeController

  public function viewProduct($id){
    $productsTR = Product::with('ProductsPhoto')->where('id',$id)->get();
    $returnHTML = view('productdetail')->with('productsTR', $productsTR)->render();
    return response()->json(array('success' => true, 'html'=>$returnHTML));
}

Route

Route::get('view-product/{id}', 'HomeController@viewProduct')->name('view.producT');

Partial Blade(productdetail)

@foreach($productsTR as $product)
@foreach($product->ProductsPhoto as $productImage)
           <img src=""  alt="small">
  @endforeach
@endforeach

Main blade

<div class="modal fade" id="view-product" tabindex="-1" role="dialog" aria- 
labelledby="myModalLabel">
<div class="modal-dialog" role="document">
    @include('productdetail')
</div>
</div>


 @foreach($products as $product)
    @if(count($product->ProductsPhoto))
    <a href="javascript:;" id="" class="modal-global" >
    <img src="" alt="" >
</a>
        @endif
    @endforeach

Javascript

 <script>
$('.modal-global').click(function(event) {
    event.preventDefault();

    var product_id = $(this).attr("id");
var url = '';
url = url.replace(':product_id', product_id);

    $("#view-product").modal('show');

    $.ajax({
        url: url,
        type: 'GET',
        dataType: 'html',
    })
    .done(function(response) {
        $("#view-product").find('.view-product').html(response);
    });

});
</script>


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire