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.
1 2 3 4 5 6 7 8 9 10 11 12 |
//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:
12345php 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
1 2 3 |
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.
123456789public 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.
2 Comments
You call this a tutorial? where are the views?controllers? You would have rather kept this to yourself than waste your time posting something that is absolutely unbeneficial to people looking to actually learn how laravel interacts with ajax calls.
2018-01-02 15:30:31Thanks for your comment.Have a complete look at the blog, there are links to Views/controllers/javascript also the link to github repo.
2018-01-02 16:27:46https://github.com/twf-nikhila/Laravel-Ajax-Crud-Operation-For-Resource-Controller