count() == 0) { $group = new ClientsGroup([ 'code' => '0', 'name' => 'Без группы', ]); $group->save(); } $countPerPage = 25; $offset = ($input['page'] - 1) * $countPerPage; if ($input['group_id'] == 0) { $group = ClientsGroup::where('code', '=', $input['group_id'])->first(); } else { $group = ClientsGroup::where('id', '=', $input['group_id'])->first(); } $clients = Client::where('group_id', '=', $group['code'])->skip($offset)->take($countPerPage)->get(); $total = Client::where('group_id', '=', $group['code'])->count(); if ($total == NULL) { return [ 'status' => 'success', 'clients' => array(), 'total' => 0, 'size' => $countPerPage, 'pages' => 1, 'currentGroup' => $group['id'], ]; } else { foreach ($clients as $client) { $phone = ClientsPhone::where('client_guid', '=', $client['user_code'])->first(); $out[] = array('id' => $client['id'], 'name' => $client['name'], 'phone' => $phone['phone']); } return [ 'status' => 'success', 'clients' => $out, 'total' => $total, 'size' => $countPerPage, 'pages' => ceil($total / $countPerPage), 'currentGroup' => $group['id'], ]; } } }