N
N
Nubbb2020-07-13 15:00:43
Laravel
Nubbb, 2020-07-13 15:00:43

Search in Laravel collection?

There is a feature table

5f0c4c40304a7694809475.png

$compares = Compare::where('gender','=',$user->attributes->gender)->get();


How can I find data from a collection without creating additional queries?

now I have like this, but I need to get rid of so many requests

$hair_color = Compare::where('value','=', $user->attributes->hair_color)->where('type','=', 3)->first();
        if ($hair_color){
            $appearances.=$hair_color->content.',';
        }
        $eye_color = Compare::where('value','=', $user->attributes->eye_color)->where('type','=', 4)->first();
        if ($eye_color){
            $appearances.=$eye_color->content.',';
        }
        $appearance = Compare::where('value','=', $user->attributes->appearance)->where('type','=', 5)->first();
        if ($appearance){
            $appearances.=$appearance->content.',';
        }
        $body_type = Compare::where('value','=', $user->attributes->body_type)->where('type','=', 6)->first();
        if ($body_type){
            $appearances.=$body_type->content.',';
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2020-07-13
@Alex_Wells

Open the documentation and read about the query builder.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question