Skip to content

Instantly share code, notes, and snippets.

@aynm142
Last active May 16, 2018 12:05
Show Gist options
  • Save aynm142/f740901e9168501a2f788006f3d97857 to your computer and use it in GitHub Desktop.
Save aynm142/f740901e9168501a2f788006f3d97857 to your computer and use it in GitHub Desktop.
task.php
public function newTasks(Request $request)
{
// callback for return all tasks
$returnResponse = function($tasks) {
$response = [];
foreach ($tasks as $task) {
$response[] = [
'id' => $task->id,
'package_name' => $task->package_name,
'title' => $task->title,
'status' => $task->status,
'image_url' => $task->image_url,
'award' => $task->award,
'daily_award' => $task->daily_arard,
'type' => $task->type,
'amount' => $task->amount,
'keywords' => $task->keywords,
];
}
return $response;
};
// get viewed tasks
$user_tasks = $this->user->tasks()->whereIsChecked(1)->get();
// if tasks array is not empty
if ($user_tasks->isNotEmpty()) {
// get theirs ids
$taskIds = $user_tasks->pluck('id');
// get only not checked tasks
$notCheckedTasks = Task::whereNotIn('id', $taskIds)->limit($request->get('start'))->offset($request->get('offset'))->get();
return new JsonResponse(
$returnResponse($notCheckedTasks),
200);
}
// get only completed tasks
$user_tasks = $this->user->tasks()->whereDone(1)->get();
// get theirs ids
$taskIds = $user_tasks->pluck('id');
// get only not checked tasks
$notCheckedTasks = Task::whereNotIn('id', $taskIds)->limit($request->get('start'))->offset($request->get('offset'))->get();
return new JsonResponse(
$returnResponse($notCheckedTasks),
200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment