php - Fetch data from table, then change something Laravel
Get the solution ↓↓↓I have aPerson
model andPersonController
for above picturedPeople
table.
I want to select all from table and order by rank (rank is who has more points) like this:
$people = Person::orderBy('rank','desc')->get();
My issue lies in the fact that, when I order by rank, I want to add the resulting position (1, 2, 3, ...) as a column in my result.
PS: in the above picture of my table, the person named Luana that has most of points should have the position 1, then the person named sadasda, at position 2, etc.
Answer
Solution:
I don't understand what the result you are referring to, when you use the get request, all the columns in the table have been taken out. If you need to add some other fields, you can use the map method of Collection
$people = Person::orderBy('rank','desc')->get();
$people->map(function($item){
$item->test = 1;
});
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: malformed utf-8 characters, possibly incorrectly encoded
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.