\$\begingroup\$
\$\endgroup\$
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
1 Answer 1
\$\begingroup\$
\$\endgroup\$
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
lang-php