简体   繁体   English

Laravel 中的动态引导模式

[英]Dynamic Bootstrap Modal in Laravel

I want to show product details in Bootstrap Modal dynamically.我想在 Bootstrap Modal 中动态显示产品详细信息。 Here is my controller这是我的 controller

$products = Product::all();
return view('landing')->with('products', $products);

And my view而我的看法

@foreach($products as $index => $product)
 <div class="product-text col-lg-6">
  <h3><a href="#">{{$product->name}}</a></h3>
   <div class="product-meta">
    <p>{{$product->details}}</p>
   </div>
   <a data-toggle="modal" data-target="{{ $index }}">Modal</a>
  </div>
@endforeach
      
    <div class="modal fade" id="{{ $index }}" role="dialog">
        <div class="modal-dialog modal-dialog-centered modal-lg">
        
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
            <h4 class="modal-title">{{$product->name}}</h4>
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            
            </div>
            <div class="modal-body">
            <p>{{$product->description}}</p>
            </div>
            <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
        
        </div>
    </div>

How can I show details for specific product?如何显示特定产品的详细信息?

We can use 2 methods for this solution.我们可以使用 2 种方法来解决此问题。

  1. use foreach to include the model code.使用foreach包括 model 代码。 Actually, this is not a good way.实际上,这不是一个好方法。 It takes more time to load the page and this is not a good practice加载页面需要更多时间,这不是一个好习惯

  2. Use ajax .使用ajax This is the best way to show your product details on a model by click on the Button这是通过单击按钮在 model 上显示您的产品详细信息的最佳方式

So you can use the second way.所以你可以使用第二种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM