Difficulties in Route Creation and Laravel Controller 5.1

Asked

Viewed 344 times

1

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

inserir a descrição da imagem aqui

Today the CRUD is 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 for the Controller to do the job but it’s 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);

    }


}
  • I took a look at the source code of Laravel and, if I’m not mistaken,, Route::controller was deprecated. Can be removed in future versions.

  • https://github.com/laravel/framework/commit/98fb934b669c45aad6ca118d4fe29f2bf141d7f6

1 answer

3

To create CRUD with controllers, I suggest you use the method Route::resouce.

It is quite 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 on the route:

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

A good reference is this video from Laracast - in English that talks about the use of this resource resource.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.