Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load blade or php content into a view via ajax/jquery in laravel?

As the title says, I am trying to dynamically load content in a view using ajax requests. I know this can be done if you are using html elements e.g. ("#div_place").html(<p>...). The problem lies when I would like to load some php/blade objects into a div for instance. Is this possible or is there a different way to go about achieving the result I want.

like image 766
Baker Johnson Avatar asked Jul 17 '14 20:07

Baker Johnson


1 Answers

This is pretty straightforward. Assuming you're using jQuery...

  1. create a route that will respond to the AJAX call and return an HTML fragment

    Route::get('test', function(){
    
        // this returns the contents of the rendered template to the client as a string
        return View::make("mytemplate")
            ->with("value", "something")
            ->render();
    
    });
    
  2. in your javascript:

    $.get(
        "test",
        function (data) {
            $("#my-content-div").html(data);
        }
    );
    
like image 148
Kryten Avatar answered Oct 23 '22 19:10

Kryten