The complete Lavarel framework development tutorial

Powered by Blogger.

Sunday 5 March 2017

Laravel CRUD operations using Resource Controller



Laravel resource controllers are helpful in case you want to handle all the HTTP request for a resource. For example if you are creating a blog then you must want to perform each and every request to a post. The resource controllers are like simple controllers, but also provides all the methods predefined for performing each and every request. In this tutorial we will learn how we can perform all the CRUD operations using resource controller.

Laravel CRUD operations using Resource Controller: Database and Model

The database design may vary depending upon requirements. In this tutorial, I am taking an example of posts table having 3 columns id, title and body.

This is enough for learning purpose. So create the database and do create the corresponding model too.

If you are not familiar with model, visit the below link to learn about models.

http://www.w3laravel.com/2016/08/eloquent-orm-laravel-models-tutorial.html

Laravel CRUD operations using Resource Controller: Creating Resource Controller and Routes

The resource controller is same as other controllers but it has predefined methods for all of the HTTP requests on a resource. For example you can have a controller that manages blog posts like creating a post, indexing all the blog post, editing, updating etc.

The make:controller is used to make a controller. We can add --resource prefix to make a resource controller.

php artisan make:controller PostController --resource
We even don't need to add individual route for each Http reuests. We can define a single resource route that will take care of all.

Route::resource('posts', 'PostController');
The following actions can be performed on a Resource Controller

Request Type URL Points to method Purpose
GET /posts index Index all the posts
GET /posts/create create Display form for creating a new Post
POST /posts store To store the post into database
GET /posts/{id} show Display a single post
GET /posts/{id}/edit edit Display a form to edit the post
PUT/PATCH /posts/{id} update Update some or all field of the post
DELETE /posts/{id} destroy Deletes a post

Laravel CRUD operations using Resource Controller: Writing Logics

Great, you have the basic idea resource controllers. Now you can learn to write logics to perform actions that is How to perform CRUD operations using resource controller.

index

You can index all the posts of a Post model using all() method.
$posts = Post::all();
And then you can return a view with all posts or simply can return json format for api calls.

create

You can simply return a view that contains a form for adding a new blog post.
return view('your-view-name');

store

To store a post data to database you have to create a new instance of Post model and then you can use save() method.
public function store(Request $request)
{
    $post = new Post;
    $post->title = $request->title;
    $post->body = $request->body;

    $post->save();
}

show

To show a particular blog post you can use the find() method with the id of the post as an argument.
$post = Post::find($id);

edit

Same as create. Display a view that contains a form to edit a post.

update

The save() method can also be used to update any model that already exists in our database. First you need to retrieve the post using find() method and then set the attributes that you want to update. And finally call the save method to update the post.
$post = Post::find($id);

$post->title = 'New Title';

$post->save();
Remember, HTML forms can only make GET and POST request. To make a PUT request from HTML form you can use method_field helper
{{ method_field('PUT') }}

destroy

Here again you need to use find() method to retrieve the post that you want to delete and finally call the delete() method to delete the post.
$post = Post::find($id);
$post->delete();
Again, you can make a delete request using method_field helper.
{{ method_field('DELETE') }}

Conclusion

These actions can also be performed without using the resource controller and resource route, but the laravel resource controller makes the task easy. You don't need to define functions, you don't have to create different routes for each request, really easy.

So, finally we can easily perform CRUD operations using resource controller on a laravel web app.

Follow us to get more and also share to help others.

0 comments:

Post a Comment