Open with fade-zoom animation

Stay ahead...
Receive latest updates directly in your Inbox!

Laravel: Ajax CRUD operation in Resource Controller

Laravel: Demo Ajax CRUD operation in Resource Controller

Laravel provides Resource Controller which is very helpful for performing CRUD (Create, Read, Update, Delete) operation for resources (like Photos, Brands, Categories etc).

CRUD Routes for resource controller can be added with just a single line of code. Also, if you want to have partial resource routes it can be easily done with only or except attributes.

Generally, most of the developer make all this CRUD operation in different pages, like Listing will be on a different page, View/Create/Edit are all on different pages. Navigation between different pages for each operation is not very time efficient for a user. So, a good solution will be to make them Ajaxified.

In this tutorial, you will find demonstrate of performing all such operation using Ajax.

Let us use Brand resource as an example here.

  • Install the Laravel package for Datatable. We will make use of yajrabox-datatables, installation instruction here
  • Install Laravel Collective’s HTML packages, which is a good package for easily creating forms with simple syntaxes. Installation instruction here
  • Create the Controller, Model and Migration file for the resource. This can be easily done with a simple one line Artisan command:

    Running this command will generate:

    • a new Brand Model (app/Brand.php)
    • Brand Controller (App/Http/Controllers/BrandController.php)
    • and a CreateBrandsTable migration (Database/migrations/create_brands_table.php).

 

  • Add the Brand route in app/routes/web.php

  • Edit the migration file and add the database fields needed. Let us assume Name and description field here.

     
  •  Edit BrandController.php to include the appropriate logics for different operations. Add ajax check in all functions so that it cannot be accessed directly, also if needed add the authorization check before accessing the resource. Find the controller code here
  • Find the View file code here
  • Find the jQuery code here

Repository: https://github.com/twf-nikhila/Laravel-Ajax-Crud-Operation-For-Resource-Controller

To create other Resources follow the similar step, simply replace the Brand with you own resource name.

Hope it helps you.

****

Let me know if there is anything I can help you with.

No Comments

Leave a comment

Your email address will not be published. Required fields are marked *