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?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 5, 2017 at 19:12
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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

answered Oct 20, 2017 at 8:17
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.