PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 20, 2022

[FIXED] Laravel sort by descending or ascending with pagination

 February 20, 2022     laravel, laravel-5, php     No comments   

Issue

I would like to perform a sort by ascending or descending together with pagination

  return \App\User:: paginate($request->per_page);

how do i add the sort by so something like this

return \App\User:: paginate($request->per_page)-sortBy(
    $request->sort_field, $request->sort_order
  );

bur the above fails where

$request->sort_order //asc or desc

the above returns an error of

"asort() expects parameter 2 to be integer, string given"

Solution

You could try something like

User::orderBy($request->sort_field, $request->sort_order)->paginate($request->per_page);

Source: https://laracasts.com/discuss/channels/laravel/combining-paginate-with-orderby



Answered By - Ron van der Heijden
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing