J
J
jazzus2019-03-10 03:28:47
Laravel
jazzus, 2019-03-10 03:28:47

How to get the ordinal number of an object from a collection?

You need to get
the Objects that belong to the user
With the serial numbers of each object from the entire collection of objects in the specified sorting.
Purpose: to show the user the positions of his objects in the public directory
Code

// Запрос все коллекции
           Project::with(..)
                  ->withCount(..)
                  ->orderBy(..)
                  ->get()
                  // добавляем номер к элементам
                  ->map(function ($item, $index) {
                       $item['number'] = $index + 1;
                       return $item;
                   })
                  // фильтруем по id авторизованного юзера
                  ->where('user_id', $user->id);

Is this the correct way or is there another one? How to integrate pagination into this system?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vism, 2019-03-10
@vism

Too lazy to write code.
Essence.
You need to make a selection with a serial number set in the database dynamically. Something like num_row, I don't remember.
Use this selection as a temporary-table and use this temporary-table to make a selection by user.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question