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 :
Next, we will create new laravel project in our localhost, to install laravel 5.3 using composer just following this command :
#Create New Project (Ajax Crud)
After finished, open it with your Text Editor like "Atom" text editor.
Don't forget to save it.
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.
Next, do migration following by this command
You have create model with name "Blog.php" and stored on app\Blog.php
blog : is an directory (blog directory) that we create for our views.
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 :
#Source code index.blade.php
#Source code create.blade.php
#Source code edit.blade.php
Your new controller will stored on app\Http\Controllers\BlogController.php
Download full source code.
see you next lessons.
at the previews lessons, we have learn how to create simple blog in laravel using Bootstrap templates, please read :
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 :
#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 :
- index.blade.php (our home crud project)
- create.blade.php (pages to create new data)
- edit.blade.php (new pages to edit data)
<!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