Skip to content
The Web Fosters: ideas into reality
Menu

Laravel: Demo Ajax CRUD operation in Resource Controller

30 July 2017, by Nikhil Agrawal

Written in 2017. Versions and links mentioned here may be out of date.

Laravel: 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.

Resource Routes
//Add routes included
Route::resource('brands', 'BrandController');

//Some routes included with only
Route::resource('brands', 'PhotoController', ['only' => [
'index', 'show'
]]);

//Some routes excluded with except
Route::resource('brands', 'PhotoController', ['except' => [
'create', 'store', 'update', 'destroy'
]]);

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:
    php artisan make:model Brand -m -r
    
    	- where Brand is the name of the resource we need.
    	- -m option indicated to "Create a new migration file for the model"
    	- -r option Indicates if the generated controller should be a resource controller

    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
Route::resource('brands', 'BrandController');

If you want to remove any CRUD function you can do it with only or except attributes.
  • Edit the migration file and add the database fields needed. Let us assume Name and description field here.
    public function up()
    {
      Schema::create('brands', function (Blueprint $table) {
         $table->increments('id');
         $table->string('name');
         $table->text('description')->nullable();
         $table->timestamps();
      });
    }

     
  •  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.