jcadima
8/22/2017 - 5:20 AM

/app/Http/Controllers/TaskController.php

/app/Http/Controllers/TaskController.php


<?php

namespace App\Http\Controllers;

use Session;
use Illuminate\Http\Request;

// import our models
use App\Project;
use App\Task;

use Illuminate\Support\Facades\Input;  // used for search
use Illuminate\Support\Facades\DB; // needed to use 'where' clause

class TaskController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $tasks = Task::all() ;
        return view('task.tasks')->with('tasks', $tasks) ;
    }

    public function list( $projectid ) {

        // dd($projectid);
        $p_name = Project::find($projectid) ;
        $task_list = DB::table('tasks')->where('project_id','=' , $projectid)->get();
        return view('task.list', compact('task_list', 'p_name') ) ;
    }


    public function sort( $key ) {

        // dd ($key) ;
        switch($key) {
            case 'task':
                $tasks = DB::table('tasks')->orderBy('task')->get();
            break;
            case 'priority':
                $tasks = DB::table('tasks')->orderBy('priority')->get();
            break;
            case 'completed':
                $tasks = DB::table('tasks')->orderBy('completed')->get();
            break;
        }

        return view('task.tasks')->with('tasks', $tasks) ;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $projects = Project::all()  ;
        return view('task.create')->with('projects', $projects) ;        
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // dd( $request->all()  ) ;
        $this->validate( $request, [
            'task'       => 'required',
            'project_id' => 'required'
        ]) ;

        $post = Task::create([
            'project_id' => $request->project_id,
            'task'       => $request->task,
            'priority'   => $request->priority
        ]);

        // ->route is defined in web.php
        Session::flash('success', 'Task Created') ;
        return redirect()->route('task.show') ; 
    }


    public function completed($id)
    {
        $task_complete = Task::find($id) ;
        $task_complete->completed = 1;
        $task_complete->save() ;
        return redirect()->back();
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $task_edit = Task::find($id) ;

        return view('task.update')->with('task_edit', $task_edit) ;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $update_task = Task::find($id) ;
        $update_task->task = $request->task; 
        $update_task->save() ;
        
        Session::flash('success', 'Task was edited') ;
        return redirect()->route('task.show') ;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $delete_task = Task::find($id) ;
        $delete_task->delete() ;
        Session::flash('success', 'Task was deleted') ;
        return redirect()->back();
    }

    public function searchTask() {
        $value = Input::get('search_task');
        $tasks = Task::where('task', 'LIKE', '%' . $value . '%')->limit(25)->get();

        return view('task.search', compact('value', 'tasks')  ) ;
    }


}