Spread the love

Part I:

Sometimes you may need to make the whole CRUD operation just in one page without reloading or navigating to another page. Live Table CRUD in Laravel can be succeeded by using Ajax.

For this course I imagine that you have already finished setting up database connectivity and your application is up. If you haven’t finished setting up you can read this article that covers all about MySQL DB Connectivity and Table manipulation on laravel

Let’s start by creating a table named Books.

php artisan make:migration create_books_table

After running the above command our new migration file will be created inside ‘database/migrations’. after I added the needed columns the content will look like:


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBooksTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('books', function (Blueprint $table) {

     * Reverse the migrations.
     * @return void
    public function down()

Now run the migrate command to create the table

php artisan migrate

After migration create model and controller. Then declare its route in web.php

php artisan make:model Book -m
php artisan make:controller BookController --resource

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Book; //is the model
use DB;

class BookController extends Controller
    public function __construct()
    public function index(Request $request)
        $books = Book::all()->toArray(); 
        return view('book.index',compact('books'));
  • Create View: create index.blade.php inside /resources/view/book directory and paste the following code.
{{ csrf_field() }}
<div id="message"></div>
<table class="table table-bordered table-striped">
       <td contenteditable="true" id="name"></td>
       <td contenteditable="true" id="author"></td>					       
       <td contenteditable="true" id="description"></td>									       
       <td><button class="btn btn-success" id="add"><i class="fa fa-plus-square"> 
       </i>Add</button></td>									</tr>
@foreach ($books as $book)										 
     <td><a id="{{$book['id']}}" style="color:white" class="delete btn btn-danger"><i class="fa fa-trash-o"></i></a></td>

I have successfully created the HTML table.

Live CRUD Operation: Create

Now, Let’s see how to insert a record to the database. As you see I have a td tag with contenteditable="true" attribute. This makes the <td></td> tag to be editable. Also I have set the id of each field so that I can get its value by its id. I have a button Add button. Next, the following is a Javascript code that listen onclick event of the add button. Also don’t forget to have a jquery plugin.

<script src="{{ asset('js/jquery.min.js') }}"></script>
<script type="text/javascript">
    var _token = $('input[name="_token"]').val();
	var name = $('#name').text(); 
	var author = $('#author').text(); 
	var description = $('#description').text();  
	if(name != ''){
			url:"{{ route('book.store') }}", 
$('#message').html("<div class='alert alert-danger'> Name field is required</div>");

Yes, This is the javascript code that will be executed when the Add button clicked. The URL “{{ route(‘book.store’) }}”, As I explained above this route is defined in web.php inside /routes directory.

Now let’s write the code for store function in BookController.php

    public function store(Request $request)
        if($request -> ajax())
            $data = array(
                'name' => $request ->name,
                'author' => $request ->author,
                'description'=> $request ->description, 
            $id = DB::table('users')->insert($data); 
            if($id > 0){
                echo '<div class="alert alert-success">Book Crated Successfully!</div>';
                echo '<div class="alert alert-danger">Error while creating the book!</div>';

After saving your project run it by using the following artisan command

php artisan serve

Then, navigate to if you are testing locally. After that fill all fields and click click the add button.

Finally, I have inserted successfully.

The part II of this lesson found here.

Categories: LaravelPHPWeb


Leave a Reply

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