Laravel 5 Crud Tutorial using Ajax & Bootstrap Template in Laravel 5.3

Laravel 5.3 tutorial : how to create CRUD (create, read, update, delete) application using Ajax and Bootstrap template in Laravel 5.3 project

Laravel 5 tutorial - Laravel 5 simple CRUD application will allow you to add new data into database, edit data, displaying all data from database, and delete data by "ID" field.

at the previews lessons, we have learn how to create simple blog in laravel using Bootstrap templates, please read :
  1. How to create Blog in Laravel 5.3
  2. How to create Bootstrap template in Laravel 5.3

Laravel 5 Simple CRUD Operations

First step - we need an database to save all data from our project, so you must create new database before create laravel project, i was created an database (MySQL database) "ajax_crud".

Next, we will create new laravel project in our localhost, to install laravel 5.3 using composer just following this command :

Laravel 5 Crud Tutorial using Ajax & Bootstrap Template in Laravel 5.3

#Create New Project (Ajax Crud)

cd c:\server\htdocs\
....
composer create-project --prefer-dist laravel/laravel AjaxCrud

After finished, open it with your Text Editor like "Atom" text editor.

#Connecting to Database

Open your laravel project "ajaxcrud" using text editor, and create connection configuration in ".ENV" file like this :

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ajax_crud
DB_USERNAME=root
DB_PASSWORD=yourpassword

Don't forget to save it.

#Create Migration

create migration using Artisan Command by following this command

php artisan make:migration create_post_table

Your migration file will stored on \database\migrations\2016_10_10_160019_create_post_table.php
Add this function into our migration file to create table and columns into database.

<?php

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

class CreatePostTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
      Schema::create('blog_post', function (Blueprint $table) {
      $table->increments('id');
      $table->string('title');
      $table->string('description');
      $table->timestamps();
      });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('blog_post');
    }
}

Next, do migration following by this command

php artisan migrate

#Create Model

Create model following by this command :

php artisan make:model Blog

You have create model with name "Blog.php" and stored on app\Blog.php

#Create Route

create new route, the route file is stored on routes\web.php

Route::group(['middleware' => ['web']], function() {
  Route::resource('blog','BlogController');  
});

blog : is an directory (blog directory) that we create for our views.

#Create Views

Next we will create views using bootstrap templates. to integrate bootstrap in laravel go to this link.
i assume you have download and install bootstrap theme in our project.

Next we create new folder ('blog") under resources\views\blog folder,


we will create three file in blog folder like :
  1. index.blade.php (our home crud project)
  2. create.blade.php (pages to create new data)
  3. edit.blade.php (new pages to edit data)
Because we using System templates, we must create "Master Template" under resources\views\master.blade.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Ajax CRUD with Laravel 5.3</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="{{ asset('/css/bootstrap.min.css') }}">
    <link rel="stylesheet" href="{{ asset('/css/font-awesome.min.css') }}">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <nav class="navbar navbar-default" role="navigation">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">AjaxCrud</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="navbar">
          <ul class="nav navbar-nav">
            <li class="active"><a href="/blog">Home</a></li>
            <li><a href="#"></a></li>

          </ul>
          <form class="navbar-form navbar-left" role="search">
            <div class="form-group">
              <input type="text" class="form-control" placeholder="Search">
            </div>
            <button type="submit" class="btn btn-default">Submit</button>
          </form>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#"></a></li>

          </ul>
        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
    </nav>
    <div class="container">
      <div class="row">
        @yield('content')
      </div>
    </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="{{ asset('/js/bootstrap.min.js') }}"></script>
  </body>
</html>

#Source code index.blade.php

@extends('master')
  @section('content')
  <div class="row">
    <div class="col-md-12">
      <h1>Simple Ajax CRUD</h1>
    </div>
  </div>
  <div class="row">
    <table class="table table-striped">
      <tr>
        <th>No.</th>
        <th>Title</th>
        <th>Description</th>
        <th>Actions</th>
      </tr>
      <a href="{{route('blog.create')}}" class="btn btn-info pull-right">Create New Data</a><br><br>
      <?php $no=1; ?>
      @foreach($blogs as $blog)
        <tr>
          <td>{{$no++}}</td>
          <td>{{$blog->title}}</td>
          <td>{{$blog->description}}</td>
          <td>
            <form class="" action="{{route('blog.destroy',$blog->id)}}" method="post">
              <input type="hidden" name="_method" value="delete">
              <input type="hidden" name="_token" value="{{ csrf_token() }}">
              <a href="{{route('blog.edit',$blog->id)}}" class="btn btn-primary">Edit</a>
              <input type="submit" class="btn btn-danger" onclick="return confirm('Are you sure to delete this data');" name="name" value="delete">
            </form>
          </td>
        </tr>
      @endforeach
    </table>
  </div>
  @stop

#Source code create.blade.php

@extends('master')
  @section('content')
  <div class="row">
    <div class="col-md-12">
      <h1>Create Data</h1>
    </div>
  </div>
  <div class="row">
    <div class="col-md-12">
      <form class="" action="{{route('blog.store')}}" method="post">
        {{csrf_field()}}
        <div class="form-group{{ ($errors->has('title')) ? $errors->first('title') : '' }}">
          <input type="text" name="title" class="form-control" placeholder="Enter Title Here">
          {!! $errors->first('title','<p class="help-block">:message</p>') !!}
        </div>
        <div class="form-group{{ ($errors->has('description')) ? $errors->first('title') : '' }}">
          <input type="text" name="description" class="form-control" placeholder="Enter Description Here">
          {!! $errors->first('description','<p class="help-block">:message</p>') !!}
        </div>
        <div class="form-group">
          <input type="submit" class="btn btn-primary" value="save">
        </div>
      </form>
    </div>
  </div>
  @stop

#Source code edit.blade.php

@extends('master')
  @section('content')
  <div class="row">
    <div class="col-md-12">
      <h1>Edit Data</h1>
    </div>
  </div>
  <div class="row">
    <form class="" action="{{route('blog.update',$blog->id)}}" method="post">
      <input name="_method" type="hidden" value="PATCH">
      {{csrf_field()}}
      <div class="form-group{{ ($errors->has('title')) ? $errors->first('title') : '' }}">
        <input type="text" name="title" class="form-control" placeholder="Enter Title Here" value="{{$blog->title}}">
        {!! $errors->first('title','<p class="help-block">:message</p>') !!}
      </div>
      <div class="form-group{{ ($errors->has('description')) ? $errors->first('title') : '' }}">
        <input type="text" name="description" class="form-control" placeholder="Enter Description Here" value="{{$blog->description}}">
        {!! $errors->first('description','<p class="help-block">:message</p>') !!}
      </div>
      <div class="form-group">
        <input type="submit" class="btn btn-primary" value="save">
      </div>
    </form>
  </div>
  @stop

#Create BlogController

After all views have create, finally we need to create new controller (BlogController.php), create new controller following this artisan command:

php artisan make:controller BlogController --resource

Your new controller will stored on app\Http\Controllers\BlogController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Blog;

class BlogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function index()
    {
        //show data
        $blogs =  Blog::all();
        return view('blog.index',['blogs' => $blogs]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return IlluminateHttpResponse
     */
    public function create()
    {
        //create new data
        return view('blog.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */
    public function store(Request $request)
    {
       // validation
      $this->validate($request,[
      'title'=> 'required',
      'description' => 'required',
    ]);
      // create new data
    $blog = new blog;
    $blog->title = $request->title;
    $blog->description = $request->description;
    $blog->save();
    return redirect()->route('blog.index')->with('alert-success','Data Hasbeen Saved!');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function edit($id)
    {
        $blog = Blog::findOrFail($id);
        // return to the edit views
        return view('blog.edit',compact('blog'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function update(Request $request, $id)
    {
        // validation
        $this->validate($request,[
          'title'=> 'required',
          'description' => 'required',
      ]);

        $blog = Blog::findOrFail($id);
        $blog->title = $request->title;
        $blog->description = $request->description;
        $blog->save();

        return redirect()->route('blog.index')->with('alert-success','Data Hasbeen Saved!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function destroy($id)
    {
        // delete data
        $blog = Blog::findOrFail($id);
        $blog->delete();
        return redirect()->route('blog.index')->with('alert-success','Data Hasbeen Saved!');
    }
}

Video Tutorial Laravel 5.3 Crud Operations


Laravel 5.3 CRUD with VueJS



Download full source code.
see you next lessons.

COMMENTS


Feel free to code it up and send us a pull request.

Hi everyone, let's me know how much this lesson can help your work. Please Subscribe and Follow Our Social Media 'kodeajaib[dot]com' to get Latest tutorials and will be send to your email everyday for free!, Just hit a comment if you have confused. Nice to meet you and Happy coding :) all ^^



Follow by E-Mail


Name

ADO.NET,3,Ajax,6,Android,9,AngularJS,4,ASP.NET,4,Blogger Tutorials,7,Bootstrap,7,C++,1,Codeigniter,2,Cplusplus,6,Crystal Report,6,CSharp,25,Ebook Java,2,FlyExam,1,FSharp,3,Game Development,2,Java,35,JDBC,2,Laravel,89,Lumen,2,MariaDB,2,Ms Access,3,MySQL,31,ODBC,6,OleDB,1,PHP,14,PHP Framework,6,PHP MYSQLI,9,PHP OOP,5,Python,8,Python 3,4,SQL Server,4,SQLite,4,Uncategorized,5,Vb 6,2,Vb.Net,89,Video,48,Vue Js,4,WPF,2,Yii,3,
ltr
item
KODE AJAIB: Laravel 5 Crud Tutorial using Ajax & Bootstrap Template in Laravel 5.3
Laravel 5 Crud Tutorial using Ajax & Bootstrap Template in Laravel 5.3
Laravel 5.3 tutorial : how to create CRUD (create, read, update, delete) application using Ajax and Bootstrap template in Laravel 5.3 project
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcWk9FSd2_jSeXkCw05Q0Lvkr2z6ERWR1lWfQeXhN13Zq03V07EK72KTr4mZu1C2RM6nsd8kGtO7ygDIfWrw03ZTVieQ2q-qH79eArkDz6JBZopGbzMmrgsrd0jraet9Zm6HAPj2DHyHg/s320/ajax-crud-in-laravel-5.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcWk9FSd2_jSeXkCw05Q0Lvkr2z6ERWR1lWfQeXhN13Zq03V07EK72KTr4mZu1C2RM6nsd8kGtO7ygDIfWrw03ZTVieQ2q-qH79eArkDz6JBZopGbzMmrgsrd0jraet9Zm6HAPj2DHyHg/s72-c/ajax-crud-in-laravel-5.png
KODE AJAIB
https://scqq.blogspot.com/2016/10/laravel-5-crud-tutorial-ajax-bootstrap.html
https://scqq.blogspot.com/
https://scqq.blogspot.com/
https://scqq.blogspot.com/2016/10/laravel-5-crud-tutorial-ajax-bootstrap.html
true
3214704946184383982
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy