/** * Delete project */ public function deleteProject($projectId, $userId) { //Get the task Ids of the project $tasks = \Task::where('project_id', $projectId)->lists('id'); if ($tasks == null) { //No Tasks. Delete data and users from the database $projectUsers = ProjectUsers::where('project_id', $projectId)->delete(); $project = Project::find($projectId); $project->deleted_by = $userId; $project->save(); $project->delete(); return true; } else { //Delete all users and data for all tasks of the project. Also delete all the users and data for the project $projectUsers = ProjectUsers::where('project_id', $projectId)->delete(); $taskUsers = TaskUser::whereIn('task_id', $tasks)->delete(); //$tasks = Task::where('project_id',$projectId)->delete(); foreach ($tasks as $taskId) { $task = \Task::find($taskId); $task->deleted_by = $userId; $task->save(); $task->delete(); } $project = Project::find($projectId); $project->deleted_by = $userId; $project->save(); $project->delete(); return true; } }