Using CRUD generator with Laravel

Laravel provides CRUD operation. Laravel resource routing assigns CRUD routes to a controller with single line of code. Using the make:controller Artisan command we can create such type of controller. Command to create model. php artisan make:model User -mrc

RESTful resource controller sets up some default routes even with the name.

Route::resource('users','UsersController');

This will create routes with names like the given below ones.

Verb Path Action Route Name

GET /users index users.index

GET /users/create create users.create

POST /users store users.store

GET /users/{user} show users.show

GET /users/{user}/edit edit users.edit

PUT|PATCH /users/{user} update users.update

DELETE /users/{user} destroy users.destroy

Set your controller with all the actions and methods you need.


class UsersController extends BaseController {


public function index() {}


public function show($id) {}


public function store() {}


}

Choose the actions and include them or exclude them as you like.


Route::resource('users','UsersController', [

'only' =>['index', 'show']

]);


Route::resource('monkeys','MonkeysController', [

'except' =>['edit', 'create']

]);

There is another type of controller called implicit controller which is more flexible. In this, based on the HTTP request type and name you get routed to your controller. It will use all sub-folders for the same route.

Route::controller('users','UserController');

Then you should set up a controller with RESTful kind of naming scheme.

class UserController extends BaseController {


public function getIndex()

{

// GET request to index

}


public function getShow($id)

{

// get request to 'users/show/{id}'

}


public function postStore()

{

// POST request to 'users/store'

}


}

It is preferred to use RESTful resource controllers instead of Implicit controllers because implicit controllers are messy and doesn’t provide names.