Compare two tables and arrange the values in a new array I have t things working with this current setup:

Route::get('test', function(){
    $finished = \App\Progress::select('stack_id')-&gt;get();
    $unfinished = \App\Stack::select(['id as stack_id'])-&gt;get();

    foreach ($unfinished as $key =&gt; $value) {
        if(!isset($finished[$key])){
            break;
        }
        if($value-&gt;stack_id == $finished[$key]-&gt;stack_id){
            unset($unfinished[$key]);
        }
    }
    return response()-&gt;json(['finished' =&gt; $finished-&gt;flatten(), 'unfinished' =&gt; $unfinished-&gt;flatten()]);
});


Two current values in progresses table and three values in stacks table

Progresses

id user_id stack_id
6   1   1
9   1   2


Stacks

id
1
2
3


The response:

{
"finished": [
{
"stack_id": 1
},
{
"stack_id": 2
}
],
"unfinished": [
{
"stack_id": 3
}
]
}


Can this be done in another, more efficient way?
you can create an array of finished and unfinished which contain stack_id and then use array_diff method in PHP.