Laravel pagination append data in one variable and other in another variable
Laravel pagination append data in one variable and other in another variable
I am facing a problem in laravel pagination. In laravel when I called paginate()
method it returns
paginate()
{
"total": 50,
"per_page": 15,
"current_page": 1,
"last_page": 4,
"first_page_url": "http://laravel.app?page=1",
"last_page_url": "http://laravel.app?page=4",
"next_page_url": "http://laravel.app?page=2",
"prev_page_url": null,
"path": "http://laravel.app",
"from": 1,
"to": 15,
"data":[
{
// Result Object
},
{
// Result Object
}
]
}
This type of Object. What I want is that I want to set data in one vairable for example $a
and except data all other value in $b
.
$a
$b
But when I added appends('data')
of my paginate variable it did not working correctly. I did not find a solution after googling it. Please help me to solve this.
appends('data')
Here is User model
<?php
namespace App;
use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateSupportFacadesAuth;
use IlluminateDatabaseEloquentSoftDeletes;
class User extends Authenticatable {
use Notifiable;
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'status', 'role_id',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
My Controller Code is
public function index() {
$users = User::where('status', 1)->paginate(10);
return response()->json(
[
'error' => 0,
'errorMsg' => '',
'data' => [
'users' => $users->appends('data')->toArray(),
],
]
);
}
I tried this code
return response()->json(
[
'error' => 0,
'errorMsg' => '',
'data' => [
'users' => $users->only($user['data'])->toArray(),
'users_pagination' => $users->except($user['data'])->toArray(),
],
]
);
In this users work correctly but users_pagination not working correctly. In both the users, users_pagination returns same value
@Gabriel I just update my question. Please take a look
– zayed hassan
Jun 29 at 9:52
I will like to see your controller how you write your code to append.
– Gabriel
Jun 29 at 9:53
@Gabriel update my question. Please take a look
– zayed hassan
Jun 29 at 9:57
@zayedhassan i have added answer, check it once
– rkj
Jun 29 at 10:19
2 Answers
2
Try this
$paginateData = User::where('status', 1)->paginate(10);
$arrPaginateData = $paginateData->toArray();
$users = $arrPaginateData['data'];
unset($arrPaginateData['data']); //remove data from paginate array
$pageInfo = $arrPaginateData;
Return in response
return response()->json(
[
'error' => 0,
'errorMsg' => '',
'data' => [
'users' => $users,
'users_pagination' => $pageInfo
],
]
);
I just updated my question please take a look
– zayed hassan
Jun 29 at 10:19
i saw your updated question, try my answer should work as your requirement
– rkj
Jun 29 at 10:21
Why not try to iterate the object? the below code will attached user specific data into each users.
public function index() {
$users = User::where('status', 1)->paginate(10);
foreach($users as $users){
$users->data = 'your data here';
}
return response()->json([
'error' => 0,
'errorMsg' => '',
'data' => [
'users' => $users,
],
]
);
}
If you want to use Laravel appends you have to follow as per the document.
I dont want to iteration over my object several times. And also I want the current_page, last_page into another variable. Not To iterate only the value
– zayed hassan
Jun 29 at 10:05
You have to work with models and relationship to use appends() function. Check the document which i have attached in my answer.
– Gabriel
Jun 29 at 10:09
I just updated my question please take a look
– zayed hassan
Jun 29 at 10:19
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You need to show your database/model query code.
– Gabriel
Jun 29 at 9:48