vendredi 2 novembre 2018

pagination with Ajax, laravel, jquery

Im trying to change my pagination of comments on my single product page from default links clicks to Ajax/Jquery link clicks. I am returning a partial in regards to the data i am returning. I am struggling to fathom where i go on from my current code and how i would define my routes to match the current system.

This is what i have so far:

Controllers:

public function getSingle($slug)
{
    //fetch from the database based on slug.
    $product = Product::where('slug', '=', $slug)->first();
    $comment = Comment::where('products_id', '=', $product->id)->orderBy('created_at', 'DESC')->paginate(2);

    $allpicture = $product->pictures;

    //return view
    return view('sights.single')->withProduct($product)->withAllpicture($allpicture)->withComment($comment);

}
public function commentsPage($slug, Request $request){
    $product = Product::where('slug', '=', $slug)->first();

    if($request->ajax())
    {
        $comment = Comment::where('products_id', '=', $product->id)->orderBy('created_at', 'DESC')->paginate(2);
        return view('partials.comments', compact('comment'))->withProduct($product)->withAllpicture($allpicture)->render();
    }
}

JS(ajax/Jquery):

UrlCommentsPage = ''
$('.container').on('click', '.pagination a', function(e){
e.preventDefault();
var url = $(this).attr('href');
$.ajax({
    url: UrlCommentsPage,
    success:function(data)
    {
        $('.comment-holder').html(data);
    }
});

});

Routes:

Route::get('products/{slug}', ['as' =>'products.single', 'uses' => 'ProductController@getSingle'])->where('slug', '[\w\d\-\_]+');
Route::get('commentspage', ['as' =>'commentspage', 'uses' => 'ProductController@commentPage']);

I am getting an error with parameters and routes can anyone see the errors?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire