Laravel:将分页添加到Json响应中

时间:2018-12-05 10:01:47

标签: php arrays json laravel

我有一个数组,用于互相显示用户的联系人列表。

我想为json响应提供add ->paginate(10)功能,但是我找不到必须放置的位置。

我的索引函数:

public function index()
{
    $contacts = [];
    $user = request()->user();
    // Loop through the contacts and format each one

    Contact::for($user->id)->get()->each(function ($contact) use ($user, &$contacts) {
        $friend = $contact->user1_id === $user->id ? $contact->user2 : $contact->user1;
        $contacts[] = $friend->toArray() + ['room' => $contact->room->toArray()];
    });

    return response()->json($contacts);
}

4 个答案:

答案 0 :(得分:1)

这是总代码

$contacts = Contact::where('user_id', $user->id)->paginate(12);
if($contacts->count()){                    
    $pageIndex = array();
    $lastPage = $contacts->lastPage();
    $user = request()->user();

    for($i= 2; $i<=$lastPage; $i++){
        array_push($pageIndex, $i);
    }

    return response()->json([
        'contacts' => $contacts->map(function ($contact) use ($user) {
                          if($contact->user1_id  === $user->id){

                            return [
                                'friend' => $contact->user2,
                                'room' => $contact->room,

                            ];

                        } else {

                            return [

                                    'friend' => $contact->user1,
                                    'room' => $contact->room,
                            ]; 

                        }
                    })->toArray(),  
        'per_page' => $contacts->perPage(),
        'on_first_page' => $contacts->onFirstPage(),
        'last_page' => $contacts->lastPage(),
        'first_page_url' => $contacts->url(1),
        'next_page_url' => $contacts->nextPageUrl(),
        'prev_page_url' => $contacts->previousPageUrl(),
        'last_page_url' => $contacts->url($contacts->lastPage()),
        'total' => $contacts->total(),
        'pageIndexArray' => $pageIndex,
        'errors' => false,
    ]);
} else {

  // Do Nothing
}

致电 GET 'URL?page='+Page_index to get the response in JS (AJAX)

答案 1 :(得分:0)

我不确定,但是尝试:将get()替换为paginate(10)

Contact::for($user->id)->paginate(10)->each(function ($contact) use ($user, &$contacts) {
        $friend = $contact->user1_id === $user->id ? $contact->user2 : $contact->user1;
        $contacts[] = $friend->toArray() + ['room' => $contact->room->toArray()];
    });

答案 2 :(得分:0)

您可以为联系人创建一个集合并使用LenfthAwarePaginator

 class ContactResourceCollection extends ResourceCollection
{
/**
 * Transform the resource into an array.
 *
 * @param  \Illuminate\Http\Request
 * @return array
 */
public function toArray($request)
{
    $response = [
        'data' => $this->collection,
    ];

    if($this->resource instanceof LengthAwarePaginator)
    {
        $response['pagination'] = [
            'total' => $this->resource->total(),
            'lastPage' => $this->resource->lastPage(),
            'perPage' => $this->resource->perPage(),
            'currentPage' => $this->resource->currentPage(),
            'nextPageUrl' => $this->resource->nextPageUrl(),
            'previousPageUrl' => $this->resource->previousPageUrl(),
        ];
    }
    return $response;
  }
}

在控制器方法中添加以下行:

 return new UserResourceCollection($users);

答案 3 :(得分:0)

您可以将查询更改为:

$contacts = Contact::for($user->id)->paginate(10);

然后,在此查询之后,您可以将for loop用于$contact;

 foreach ($contacts as $key => $contact)
 {
   $friend = $contact->user1_id === $user->id ? $contact->user2 : $contact->user1;
   $contacts[] = $friend->toArray() + ['room' => $contact->room->toArray()];
 }

在进入循环/每个循环之前先分页。