Answer the question
In order to leave comments, you need to log in
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);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question