Json Çıktımı Sayfalandırmak İstiyorum (Pagination)
10
●510
- 05-12-2018, 17:03:34Galiba tam olarak bundan bahsediyorsunuz hocam ? Stackoverflowdan cevap olarak birisi iletti bunu ama sonuç yine aynı oldu paginate yansımadı json çıktısına;bayGaReZ adlı üyeden alıntı: mesajı görüntüle
public function index() { $contact_array = []; $user = request()->user(); $contacts = Contact::for($user->id)->paginate(10); foreach ($contacts as $key => $contact) { $friend = $contact->user1_id === $user->id ? $contact->user2 : $contact->user1; $contact_array[] = $friend->toArray() + ['room' => $contact->room->toArray()]; } return response()->json($contact_array); }
Çözüm arayan olursa;
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()]; }); // Get current page form url e.x. &page=1 $currentPage = LengthAwarePaginator::resolveCurrentPage(); // Create a new Laravel collection from the array data $itemCollection = collect($contacts); // Define how many items we want to be visible in each page $perPage = 10; // Slice the collection to get the items to display in current page $currentPageItems = $itemCollection->slice(($currentPage * $perPage) - $perPage, $perPage)->all(); // Create our paginator and pass it to the view $paginatedItems= new LengthAwarePaginator($currentPageItems , count($itemCollection), $perPage); return response()->json($paginatedItems); }