Is there a way I can render Blade template & return result in AJAX ?
I have a custom template, where the first few items are loaded on page load. The remaining items are loaded via ajax. Can I use a template file to return the rendered layout via ajax ?
// Controller method
public function ajaxMethod()
{
$data = [
'view' => View::make('path/to/the/view')
->with('userLetsSay', $userData)
->render()
];
return Response::json($data, 200);
}
// AJAX callback on success.
// ...
success: function(response) {
console.log(response.view);
}
// ...
You may use something like this (Check this article):
$view = View::make('home.index')->with('something', $something);
return $view->renderSections()['content']; // Get rendered @section('content')
Also you may exclude the template for an ajax
call and return the raw data to client side and then insert that data into DOM
using JavaScript
but not sure how you are doing it so can't be more specific.
Update: You may also render the full template and can send it to client side (other answer provided it).