0
\$\begingroup\$

I have t things working with this current setup:

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

    foreach ($unfinished as $key => $value) {
        if(!isset($finished[$key])){
            break;
        }
        if($value->stack_id == $finished[$key]->stack_id){
            unset($unfinished[$key]);
        }
    }
    return response()->json(['finished' => $finished->flatten(), 'unfinished' => $unfinished->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?

\$\endgroup\$
1
\$\begingroup\$

you can create an array of finished and unfinished which contain stack_id and then use array_diff method in PHP.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.