Answer the question
In order to leave comments, you need to log in
How to improve ajax page loading code?
Hello. The site has tags. By clicking on the tag, Ajax did the loading of elements. I did it like this:
public function showCategory(Request $request, $id)
{
$recipes = Tag::find($id)->recipes()->where('public', '=', 1)->get();
$tags = Tag::with('recipes')->get();
if ($request->ajax()) {
return view('site.recipe_load', ['recipes' => $recipes])->render();
}
}
$(function() {
$('body').on('click', '.left-menu a', function(e) {
e.preventDefault();
$('#load a').css('color', '#dfecf6');
$('.preloader-wrapper').addClass('active');
var url = $(this).attr('href');
getRecipes(url);
window.history.pushState("", "", url);
});
function getRecipes(url) {
$.ajax({
url : url
}).done(function (data) {
$('.recipes').html(data).fadeIn();
console.log(data);
}).fail(function () {
alert('Recipes could not be loaded.');
});
}
});
Route::get('tag/{id}','[email protected]');
<a href="{{action('[email protected]',['id'=>$tag->id])}}">#{{$tag->name}}</a>
Answer the question
In order to leave comments, you need to log in
public function showCategory(Request $request, $id)
{
$recipes = Tag::find($id)->recipes()->where('public', '=', 1)->get();
$tags = Tag::with('recipes')->get();
if ($request->ajax()) {
return view('site.recipe_load', ['recipes' => $recipes])->render();
}
return view('site.recipe_load', ['recipes' => $recipes]);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question