简体   繁体   中英

Laravel 5.4 Checking given array from db

I'm trying to learn laravel and did some query. In this query, I'm trying to check if the values from the Record got the same year and course with the user.

$check1 = Record::where([['year','=',$show->year],['course','=',$show->course]])->get();

Since it is giving me a collection of array and wanted to check every single results that has been given, I have used a foreach.

foreach($check1 as $record){
    $check2 = Fbuser::where([['first_name','=',$record->firstname],['last_name','=',$record->lastname]])->get();
       if ($check2 === null) {
            dd($record->firstname);
       }else {
            //do something
       }}

In the code above, I was trying to check if the results from $check1 exists on Fbuser table. NOW, the problem is it would only check the first value and not the rest. How can I make every results pass through $check2 query? I thought for each would do the trick. Was planning to list all the names who aren't in the Fbuser table. Tried searching for a solution but can't seem to find one.

You can use Laravel's Filter, this will return only the Records that have a user with the same first_name and last_name:

$filtered_result = $check1->filter(function ($value, $key) {
    return Fbuser::where([['first_name','=',$value->firstname],['last_name','=',$value->lastname]];
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM