0
votes

I am facing a problem in laravel pagination. In laravel when I called paginate() method it returns

{
   "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.

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.

Here is User model

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\SoftDeletes;

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

2
You need to show your database/model query code.Gabriel
@Gabriel I just update my question. Please take a lookzayed hassan
I will like to see your controller how you write your code to append.Gabriel
@Gabriel update my question. Please take a lookzayed hassan
@zayedhassan i have added answer, check it oncerkj

2 Answers

0
votes

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
            ],
        ]
    );
0
votes

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.