I am using the "element-ui" table with vuejs, pagination, listing everything is ok, but it does it while Sort Process, but it cannot sort asc or desc because of my laravel query. laravel query can hold the query parameter, how to write it in laravel for your help in advance.
Please keep in mind that I am new to laravel when replying
query parametrs
page: 1 sort: email|asc
http://localhost:3000/user?page=1&sort=name|desc
http://localhost:3000/user?page=1&sort=email|asc
public function getUser(Request $request)
{
$users = User::paginate(10);
return response()->json($users);
}
{
"current_page": 1,
"data": [
{
"id": 1,
"name": "serdar",
"email": "[email protected]",
"email_verified_at": null,
"created_at": "2022-01-03T10:11:46.000000Z",
"updated_at": "2022-01-03T10:11:46.000000Z"
},
{
"id": 2,
"name": "Nettie Muller",
"email": "[email protected]",
"email_verified_at": "2022-01-05T09:34:01.000000Z",
"created_at": "2022-01-05T09:34:02.000000Z",
"updated_at": "2022-01-05T09:34:02.000000Z"
},
{
"id": 3,
"name": "Bridgette Blanda",
"email": "[email protected]",
"email_verified_at": "2022-01-05T09:34:01.000000Z",
"created_at": "2022-01-05T09:34:02.000000Z",
"updated_at": "2022-01-05T09:34:02.000000Z"
},
{
"id": 4,
"name": "Braden Marvin",
"email": "[email protected]",
"email_verified_at": "2022-01-05T09:34:01.000000Z",
"created_at": "2022-01-05T09:34:02.000000Z",
"updated_at": "2022-01-05T09:34:02.000000Z"
},
{
"id": 5,
"name": "Donna Gutmann",
"email": "[email protected]",
"email_verified_at": "2022-01-05T09:34:01.000000Z",
"created_at": "2022-01-05T09:34:02.000000Z",
"updated_at": "2022-01-05T09:34:02.000000Z"
},
{
"id": 6,
"name": "Ms. Viola Ullrich PhD",
"email": "[email protected]",
"email_verified_at": "2022-01-05T09:34:01.000000Z",
"created_at": "2022-01-05T09:34:02.000000Z",
"updated_at": "2022-01-05T09:34:02.000000Z"
},
{
"id": 7,
"name": "Christy Lakin",
"email": "[email protected]",
"email_verified_at": "2022-01-05T09:34:01.000000Z",
"created_at": "2022-01-05T09:34:02.000000Z",
"updated_at": "2022-01-05T09:34:02.000000Z"
},
{
"id": 8,
"name": "Creola Sporer",
"email": "[email protected]",
"email_verified_at": "2022-01-05T09:34:01.000000Z",
"created_at": "2022-01-05T09:34:02.000000Z",
"updated_at": "2022-01-05T09:34:02.000000Z"
},
{
"id": 9,
"name": "Ms. Kyra Glover MD",
"email": "[email protected]",
"email_verified_at": "2022-01-05T09:34:01.000000Z",
"created_at": "2022-01-05T09:34:02.000000Z",
"updated_at": "2022-01-05T09:34:02.000000Z"
},
{
"id": 10,
"name": "Catherine Denesik MD",
"email": "[email protected]",
"email_verified_at": "2022-01-05T09:34:01.000000Z",
"created_at": "2022-01-05T09:34:02.000000Z",
"updated_at": "2022-01-05T09:34:02.000000Z"
}
],
"first_page_url": "http://kelebek.localhost/kasalar?page=1",
"from": 1,
"last_page": 11,
"last_page_url": "http://kelebek.localhost/kasalar?page=11",
"links": [
{
"url": null,
"label": "« Previous",
"active": false
},
{
"url": "http://kelebek.localhost/kasalar?page=1",
"label": "1",
"active": true
},
{
"url": "http://kelebek.localhost/kasalar?page=2",
"label": "2",
"active": false
},
{
"url": "http://kelebek.localhost/kasalar?page=3",
"label": "3",
"active": false
},
{
"url": "http://kelebek.localhost/kasalar?page=4",
"label": "4",
"active": false
},
{
"url": "http://kelebek.localhost/kasalar?page=5",
"label": "5",
"active": false
},
{
"url": "http://kelebek.localhost/kasalar?page=6",
"label": "6",
"active": false
},
{
"url": "http://kelebek.localhost/kasalar?page=7",
"label": "7",
"active": false
},
{
"url": "http://kelebek.localhost/kasalar?page=8",
"label": "8",
"active": false
},
{
"url": "http://kelebek.localhost/kasalar?page=9",
"label": "9",
"active": false
},
{
"url": "http://kelebek.localhost/kasalar?page=10",
"label": "10",
"active": false
},
{
"url": "http://kelebek.localhost/kasalar?page=11",
"label": "11",
"active": false
},
{
"url": "http://kelebek.localhost/kasalar?page=2",
"label": "Next »",
"active": false
}
],
"next_page_url": "http://kelebek.localhost/kasalar?page=2",
"path": "http://kelebek.localhost/kasalar",
"per_page": 10,
"prev_page_url": null,
"to": 10,
"total": 101
}
CodePudding user response:
try
$users = User::orderBy('name', 'DESC')->paginate(10);
CodePudding user response:
use this if you want to order by asc
public function getUser(Request $request)
{
$var = $request->input('var', 'id');
$users = User::orderBy($var,'asc')->paginate(10);
return response()->json($users);
}
and if you want to order by desc
public function getUser()
{
$var = $request->input('var', 'id');
$users = User::orderBy($var,'desc')->paginate(10);
return response()->json($users);
}
