Laravel dynamic data populate in bootstrap modal
10,005
Solution 1
You can load content for bootstrap modal via jQuery ajax
<div id="view-modal" class="modal fade"
tabindex="-1" role="dialog"
aria-labelledby="myModalLabel"
aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close"
data-dismiss="modal"
aria-hidden="true">
×
</button>
<h4 class="modal-title">
<i class="glyphicon glyphicon-user"></i> User Profile
</h4>
</div>
<div class="modal-body">
<div id="modal-loader"
style="display: none; text-align: center;">
<img src="ajax-loader.gif">
</div>
<!-- content will be load here -->
<div id="dynamic-content"></div>
</div>
<div class="modal-footer">
<button type="button"
class="btn btn-default"
data-dismiss="modal">
Close
</button>
</div>
</div>
</div>
</div><!-- /.modal -->
<button data-toggle="modal" data-target="#view-modal"
id="getUser" class="btn btn-sm btn-info"
data-url="{{ route('dynamicModal',['id'=>$id])}}"
>
Submit
</button>
<script>
$(document).ready(function(){
$(document).on('click', '#getUser', function(e){
e.preventDefault();
var url = $(this).data('url');
$('#dynamic-content').html(''); // leave it blank before ajax call
$('#modal-loader').show(); // load ajax loader
$.ajax({
url: url,
type: 'GET',
dataType: 'html'
})
.done(function(data){
console.log(data);
$('#dynamic-content').html('');
$('#dynamic-content').html(data); // load response
$('#modal-loader').hide(); // hide ajax loader
})
.fail(function(){
$('#dynamic-content').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...');
$('#modal-loader').hide();
});
});
});
</script>
In route file
Route::get('dynamicModal/{id}',[
'as'=>'dynamicModal',
'uses'=> 'ControllerName@loadModal'
]);
In the controller
public function loadModal($id)
{
// write your process if any
return view('view_having_model_body',['data'=>$ifAnyData]);
}
Solution 2
Another way to do this dynamically, add your product id to the data-target like
<a href="#"
data-title="Lorem Ipsum is simply"
data-old-price="$359.99"
data-special-price="$329.99"
data-toggle="modal"
data-target="#modal-quickview{{$product->id}}"
>
do the same thing to the modal body id like
<div id="modal-quickview{{$product->id}}"
class="modal fade"
tabindex="-1"
role="dialog"
aria-labelledby="myModalLabel"
aria-hidden="true"
style="display: none;"
>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
In this case laravel generates a unique id for every modal. In the modal body you just have to display product based on their id
Author by
User57
Updated on June 24, 2022Comments
-
User57 almost 2 years
My bootstrap modal i have placed in dashboard. And the button which will populate the modal i placed in another page and in there I extends the dashboard. When i click on different product id button, product related information should comes up in modal. My question is how do i make it dynamic. means the bootstrap modal will populate based on product i clicked for quick view.
**button for quick view in index.blade.php :** <div class="btn-quickview"> <a href="#" data-toggle="modal" data-target="#modal-quickview"> <i class="fa fa-search-plus" aria-hidden="true"> </i> Quick View </a> </div> **Modal placed in dashboard.blade.php** <div id="modal-quickview" class="modal fade" role="dialog"> <div class="modal-dialog"> <div class="modal-body"> <button type="button" class="close myclose" data-dismiss="modal">×</button> <div class="product-view-area"> <div class="col-xs-12 col-sm-7 col-lg-7 col-md-7 product-details-area"> <div class="product-name"> <h2>Lorem Ipsum is simply</h2> </div> <div class="price-box"> <p class="special-price"> <span class="price-label">Special Price</span> <span class="price"> $329.99 </span> </p> <p class="old-price"> <span class="price-label">Regular Price:</span> <span class="price"> $359.99 </span> </p> </div> </div> </div> </div> <div class="modal-footer"> <a href="#" class="btn-services-shop-now" data-dismiss="modal">Close</a> </div> </div> </div> </div>