Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to find the position of a specific row in a laravel collection(already fetched)

New member
Joined
Feb 9, 2023
Messages
3
I need the position of a particular row from an already fetched the collection of models in laravel
$data=Lessions::where('status',1)->where('teacher_id',56)->get(); how to find the position of $data->where('student_id',3) We can do it by foreach loop. anyone can suggest the more optimized way, without using foreach loop. Note: i want position(from top to bottom) not id.
 
New member
Joined
Feb 9, 2023
Messages
4
You can do this from many ways. First which i remember use Laravel Helper.

Code:
use Illuminate\Support\Arr;
$filtered = Arr::where($array, function ($value, $key) {
    return $value->student_id == 3 ? $value: '';
});

Also you can use map helper as well.

Code:
$mapped = Arr::map($array, function ($value, $key) {
    return $value->student_id == 3 ? $value: '';
});
 
Top