Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hassank's avatar

Filter non-eloquent collection

How to filter non eloquent collection in laravel .

My code $student = collect($students);

$students is comming from third-party service which return an array.

so now i want to filter this collection by name .

but collection methods are not working on this because this is non-eloquent . $studentform->filter(request('search')['value'])

or

$studentform->find(request('search')['value'])

or $studentforms->filter(function ($data) { if( $data->FullName = request('search')['value']) return $data; });

i get this error

Call to a member function getQuery() on null

0 likes
4 replies
tykus's avatar

If $students is an array (of arrays), then the object operator (->) would be invalid in this case $data->FullName.

It's difficult to understand exactly what you are trying to do, and without a wider context it is impossible to give you an answer. Maybe consider something like the following

$studentforms->filter(function ($data) {
    return  $data['FullName'] == request('search')['value'];
});
hassank's avatar

as i mentioned

$students

is an array and laravel collect() method is applied to it .

$studentforms = collect($students);

in this case $studentforms is collection

now as $studentforms is non-eloquent collection how am i gona filter that .

Sirik's avatar

Collections (https://laravel.com/docs/5.6/collections) is not the same as Eloquent Collections (https://laravel.com/docs/5.6/eloquent-collections)

I think you're looking for this https://laravel.com/docs/5.6/collections#method-mapwithkeys

$collection = collect([
    [
        'name' => 'John',
        'department' => 'Sales',
        'email' => '[email protected]'
    ],
    [
        'name' => 'Jane',
        'department' => 'Marketing',
        'email' => '[email protected]'
    ]
]);

$keyed = $collection->mapWithKeys(function ($item) {
    return [$item['email'] => $item['name']];
});

$keyed->all();

/*
    [
        '[email protected]' => 'John',
        '[email protected]' => 'Jane',
    ]
*/
Snapey's avatar

you need to show an example of your data.

Also, try to be careful with your code, you have $student, $studentform and $studentforms in your failing examples.

How can we be confident that you have not just spelled the variable wrong?

Please or to participate in this conversation.