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

As the title says, I'm trying to dynamically load content in a form using ajax requests. I know this can be done if you use html elements, for example. ("#div_place").html(<p>...) . The problem is that I would like, for example, loading some php / blade server objects into a div. Is this possible or is there another way to achieve the desired result.

+6
source share
2 answers

It is pretty simple. Assuming you are using jQuery ...

  • create a route that will answer the AJAX call and return the 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(); }); 
  • in your javascript:

     $.get( "test", function (data) { $("#my-content-div").html(data); } ); 
+4
source

After some digging, I found this one that helped me solve the problem.

You must use .load("url/page/...") via jquery, and then set the route in the route.php file, which displays a view with the data you need to load, for example.

 Route::get('/url/page/...', function(){ return View::make('test'); }); 

This returns the necessary PHP code that needs to be loaded dynamically, greetings.

0
source

Source: https://habr.com/ru/post/972412/


All Articles