Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel @include a blade view via ajax

I have a page which will @include some content, I want to @include that blade view file using ajax request. How should I do it.

Basically, the view file will get the items from server,

**price.blade.php**
@foreach ($items as $item)
<div class="item-post">
  <div class="priceofitem">{{ $item->price }} </div>

I want to include @include('price.blade.php') in my tab section

<ul class="tabs">
<li><a href="#tab1">Prices </li>
<div id="tab1">@include('price.blade.php')</div>

I don't want to include that view file automatically upon load, as I don't want to load that tab's content unless the user clicks on it, if the user wants the prices than the user clicks on that tab, and an AJAX request will be sent to include that file.

Hope I made myself clear, please let me know if you did not understand me.

Fingers crossed

like image 274
Cowgirl Avatar asked Sep 27 '17 13:09

Cowgirl


2 Answers

You want something like

$(document).ready(function() {
    $("#tab1").click(function() {
        $.ajax({
            type: 'POST', 
            url : "/yourrouteview", 
            success : function (data) {
                $("#tab1").html(data);
            }
        });
    });
}); 

Your controller and route must configure /yourrouteview to get the correct view (that is @include('price.blade.php')

like image 124
Dhiraj Avatar answered Oct 18 '22 19:10

Dhiraj


Make your ajax request and return the view from controller function like:

return view('your_view');

in ajax success function, append it to where you want like:

success: function(response){
    $('#Id').html(response);
}

The flow is something like:

$('#tab').click(function(){
    // ajax call here
    ...
    success: function(response){
        $('#Id').html(response);
    }
});

controller:

function funcName()
{
    // Do what ever you want
    return view('your_view');
}
like image 44
Mayank Pandeyz Avatar answered Oct 18 '22 19:10

Mayank Pandeyz