Difficulties in Creating Routes and Controller Laravel 5.1

1

I created a CRUD with Modal is working everything right but I created form request data view, queries with BD all inside Routes, I do not think very cool would like to pass these features to the Controller to do but is not working is appearing the error:

Today CRUD looks like this:

app \ routes.php

use App\Task;
use Illuminate\Http\Request;


Route::get('/admin', function () {
    $tasks = Task::all();

    return View::make('admin')->with('tasks',$tasks);
});



Route::get('/tasks/{task_id?}',function($task_id){
    $task = Task::find($task_id);

    return Response::json($task);
});

Route::post('/tasks',function(Request $request){
    $task = Task::create($request->all());

    return Response::json($task);
});

Route::put('/tasks/{task_id?}',function(Request $request,$task_id){
    $task = Task::find($task_id);

    $task->task = $request->task;
    $task->description = $request->description;

    $task->save();

    return Response::json($task);
});

Route::delete('/tasks/{task_id?}',function($task_id){
    $task = Task::destroy($task_id);

    return Response::json($task);
});

Then I created this way so that the Controller does the work but it is not working

app \ routes.php

use App\Task;
use Illuminate\Http\Request;


Route::controller('/admin', 'TaskController');

controllers \ TaskController.php

namespace App\Http\Controllers;

use App\Task;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class TaskController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getindex()
    {
        $tasks = Task::all();

        return View::make('admin')->with('tasks',$tasks);


    }

    public function getAdd($task_id)
    {
        $task = Task::find($task_id);

        return Response::json($task);
    }

    public function postAdd(Request $request)
    {

        $task = Task::create($request->all());

        return Response::json($task);

    }


    public function getEdit($task_id)
    {
        $task = Task::find($task_id);

        return Response::json($task);
    }

    public function postEdit(Request $request, $task_id)
    {

       // $task = $request->except('_token');

        $task = Task::find($task_id);

        $task->task = $request->task;
        $task->description = $request->description;

        $task->save();

        return Response::json($task);


    }

    public function getDelete($task_id)
    {
        $task = Task::destroy($task_id);

        return Response::json($task);

    }


}
    
asked by anonymous 11.04.2016 / 22:01

1 answer

3

For creating CRUD with controllers, I suggest you use the Route::resouce method.

It is very simple, and you just need to have a controller with the following methods:

 (get) index
 (get) edit
 (post) update
 (get) create
 (post) store
 (delete) destroy

Just use the code below in the route:

 Route::resource('tasks', 'TasksController');

A good reference is this Laracast video that talks about using this resource resource .

    
12.04.2016 / 13:32