Thứ Ba, 6 tháng 12, 2016

Laravel 5 Tutorial : Laravel 5.3 Simple CRUD Application With Angular JS


Laravel 5.3 tutorial for beginners : Create simple Laravel 5.3 CRUD Operations with laravel 5.3 and Angular JS backend (REST API). At the previews lessons, we have create simple crud with laravel too, please read :
  1. Laravel 5.3 Crud Operation with Ajax
  2. Laravel 5.3 Crud Application with Resource Controller
  3. Laravel 5.3 Crud Opeartion with Vue.Js Full video
This lessons will use Laravel 5 for the backend and AngularJS for the front end. In accordance with the laws of beauty, we will use twitter bootstrap to add beauty to our simple application.

Video Tutorial Laravel CRUD with Angular Js


Create New Laravel Project

cd c:\server\htdocs\
....
composer create-project laravel/laravel laravelangularjs

Connect with Database (MySQL Database)

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravelangularjs
DB_USERNAME=root
DB_PASSWORD=yourpasswqord

Create Migration and Tables

php artisan make:migration create_supplier_table

Create tables

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSupplierTable extends Migration {

    public function up(){
        // create supplier table
        Schema::create('supplier', function (Blueprint $table) {
         $table->increments('id');
         $table->string('supplierName')->unique();
         $table->string('supplierEmail')->unique();
         $table->string('supplierContact');
         $table->string('supplierPosition');
         $table->timestamps();
        });
    }

    public function down() {
        // Drop supplier table
        Scheme::drop('supplier');
    }
}

Run Migration

php artisan migrate

Create Model

php artisan make:model Supplier

Supplier.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Supplier extends Model{
    protected $table ='supplier';
    protected $fillable = array('id','supplierName','supplierEmail','supplierContact','supplierPosition');
}

Create Controller

php artisan make:controller SupplierController --resource

SupplierController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Supplier; // use our supplier model

class SupplierController extends Controller {

    public function index($id = null) {
      if ($id == null){
        return Supplier::orderBy('id', 'asc')->get();
      } else {
        return $this->show($id);
      }
    }

    public function store(Request $request) {
        $supplier = new Supplier;
        $supplier->supplierName = $request->input('supplierName');
        $supplier->supplierEmail = $request->input('supplierEmail');
        $supplier->supplierContact = $request->input('supplierContact');
        $supplier->supplierPosition = $request->input('supplierPosition');
        $supplier->save();
        return 'Supplier record successfully created with id' . $supplier->id;
    }
    public function show($id) {
        return Supplier::find($id);
    }
    public function update(Request $request, $id) {
        $supplier = Supplier::find($id);
        $supplier->supplierName = $request->input('supplierName');
        $supplier->supplierEmail = $request->input('supplierEmail');
        $supplier->supplierContact = $request->input('supplierContact');
        $supplier->supplierPosition = $request->input('supplierPosition');
        $supplier->save();
        return 'Supplier record successfully updated with id ' . $supplier->id;
    }
    public function destroy($id) {
        $supplier = Supplier::find($id)->delete();
        return 'Employee record successfully deleted';
    }
}

Create New Routes

Route::get('/', function () {
    return view('index');
});
Route::get('/api/supplier/{id?}', 'SupplierController@index');
Route::post('/api/supplier', 'SupplierController@store');
Route::post('/api/supplier/{id}', 'SupplierController@update');
Route::delete('/api/supplier/{id}', 'SupplierController@destroy');

Create New Views (index.blade.php)

<!DOCTYPE html>
<html lang="en" ng-app="getSupplier">
  <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>Laravel 5 and Angular CRUD Application</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/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>

    <div class="container">
      <h2>Simple CRUD Application with AngularJs</h2>
      <div ng-controller="SupplierController">
        <table class="table table-striped">
          <thead>
            <tr>
              <th>ID</th>
              <th>Supplier Name</th>
              <th>Supplier Email</th>
              <th>Supplier Contact</th>
              <th>Supplier Position</th>
              <th>
                <button id="btn-add" class="btn btn-success btn-xs" ng-click="toggle('add', 0)">Add New Supplier</button>
              </th>
            </tr>
          </thead>
          <tbody>
            <tr ng-repeat="supplier in suppliers">
              <td>@{{ supplier.id }}</td>
              <td>@{{ supplier.supplierName }}</td>
              <td>@{{ supplier.supplierEmail }}</td>
              <td>@{{ supplier.supplierContact }}</td>
              <td>@{{ supplier.supplierPosition }}</td>
              <td>
                <button class="btn btn-warning btn-xs btn-detail" ng-click="toggle('edit', supplier.id)">
                  <span class="glyphicon glyphicon-edit"></span>
                </button>
                <button class="btn btn-danger btn-xs btn-delete" ng-click="confirmDelete(supplier.id)">
                  <span class="glyphicon glyphicon-trash"></span>
                </button>
              </td>
            </tr>
          </tbody>
        </table>
        <!-- show modal  -->
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">@{{form_title}}</h4>
              </div>
              <div class="modal-body">
                <form name="frmSupplier" class="form-horizontal" novalidate="">
                  <div class="form-group">
                    <label class="col-sm-3 control-label">Supplier Name</label>
                    <div class="col-sm-9">
                      <input type="text" class="form-control" id="supplierName" name="supplierName" placeholder="Supplier Name" value="@{{supplierName}}" ng-model="supplier.supplierName" ng-required="true">
                      <span ng-show="frmSupplier.supplierName.$invalid && frmSupplier.supplierName.$touched">Supplier Name field is required</span>
                    </div>
                  </div>

                    <div class="form-group">
                      <label class="col-sm-3 control-label">Supplier Email</label>
                      <div class="col-sm-9">
                        <input type="email" class="form-control" id="supplierEmail" name="supplierEmail" placeholder="Supplier Email" value="@{{supplierEmail}}" ng-model="supplier.supplierEmail" ng-required="true">
                        <span ng-show="frmSupplier.supplierEmail.$invalid && frmSupplier.supplierEmail.$touched">Supplier Email field is required</span>
                      </div>
                    </div>

                    <div class="form-group">
                      <label class="col-sm-3 control-label">Supplier Contact</label>
                      <div class="col-sm-9">
                        <input type="text" class="form-control" id="supplierContact" name="supplierContact" placeholder="Supplier Contact" value="@{{supplierContact}}" ng-model="supplier.supplierContact" ng-required="true">
                        <span ng-show="frmSupplier.supplierContact.$invalid && frmSupplier.supplierContact.$touched">Supplier Contact field is required</span>
                      </div>
                    </div>

                    <div class="form-group">
                      <label class="col-sm-3 control-label">Supplier Position</label>
                      <div class="col-sm-9">
                        <input type="text" class="form-control" id="supplierPosition" name="supplierPosition" placeholder="Supplier Position" value="@{{supplierPosition}}" ng-model="supplier.supplierPosition" ng-required="true">
                        <span ng-show="frmSupplier.supplierPosition.$invalid && frmSupplier.supplierPosition.$touched">Supplier Position field is required</span>
                      </div>
                    </div>
                </form>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="btn-save" ng-click="save(modalstate, id)" ng-disabled="frmSupplier.$invalid">Save Changes</button>
              </div>
            </div>
          </div>
        </div>
      </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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

    <!-- Aangular Material load from CDN -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.1/angular-material.min.js"></script>

    <!-- Angular Application Scripts Load  -->
    <script src="{{ asset('angular/app.js') }}"></script>
    <script src="{{ asset('angular/controllers/SupplierController.js') }}"></script>
  </body>
</html>

AngularJS application structure

Laravel 5 Tutorial : Laravel 5.3 Simple CRUD Application With Angular JS

Create new folder (angular) in public\angular, and create new file (app.js) in there (public\angular\app.js)

public\angular\app.js

var app = angular.module('getSupplier', [])
        .constant('API_URL','http://localhost:8080/api/');

Create new folder (controller) in public\angular directory, and create new file (SupplierController.js) in public\angular\controller\SupplierController.js folder

public\angular\controller\SupplierController.js

app.controller('SupplierController', function($scope, $http, API_URL) {
  // retrieve Supplier listing from API
  $http.get(API_URL + "supplier")
  .success(function(response){
    $scope.suppliers = response;
  });

  // show modal Form
  $scope.toggle = function(modalstate, id) {
      $scope.modalstate = modalstate;
      switch(modalstate) {
        case 'add':
          $scope.form_title = "Add New Supplier";
          break;
        case 'edit':
          $scope.form_title = "Supplier Detail";
          $scope.id = id;
          $http.get(API_URL + 'supplier/' + id).success(function(response){
            console.log(response);
            $scope.supplier = response;
          });
          break;
        default:
          break;
      }
      console.log(id);
      $('#myModal').modal('show');
  }

  // save new supplier and update existing supplier
  $scope.save = function(modalstate, id) {
    var url = API_URL + "supplier";
    if (modalstate === 'edit') {
      url += "/" + id;
    }
    $http({
      method: 'POST',
      url: url,
      data: $.param($scope.supplier),
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function(response){
      console.log(response);
      location.reload();
    }).error(function(response){
      console.log(response);
      alert('This is embarassing. An error has occured. Please check the log for details');
    });
  }

 // delete supplier record
 $scope.confirmDelete = function(id) {
   var isConfirmDelete = confirm('Are you sure you want this record?');
   if (isConfirmDelete) {
     $http({
       method: 'DELETE',
       url: API_URL + 'supplier/' + id
     }).success(function(data){
       console.log(data);
       location.reload();
     }).error(function(data){
       console.log(data);
       alert('Unable to delete');
     });
   } else {
     return false;
   }
 }
});

Watch more Laravel videos

Laravel 5.3 CRUD with VueJS



Laravel 5.3 CRUD with AJAX


DOWNLOAD FULL SOURCE CODE Laravel 5.3 Simple CRUD Application With Angular JS HERE!!!.

See you next lessons ..

Laravel 5 Tutorial : Laravel 5.3 Simple CRUD Application With Angular JS


Laravel 5.3 tutorial for beginners : Create simple Laravel 5.3 CRUD Operations with laravel 5.3 and Angular JS backend (REST API). At the previews lessons, we have create simple crud with laravel too, please read :
  1. Laravel 5.3 Crud Operation with Ajax
  2. Laravel 5.3 Crud Application with Resource Controller
  3. Laravel 5.3 Crud Opeartion with Vue.Js Full video
This lessons will use Laravel 5 for the backend and AngularJS for the front end. In accordance with the laws of beauty, we will use twitter bootstrap to add beauty to our simple application.

Video Tutorial Laravel CRUD with Angular Js


Create New Laravel Project

cd c:\server\htdocs\
....
composer create-project laravel/laravel laravelangularjs

Connect with Database (MySQL Database)

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravelangularjs
DB_USERNAME=root
DB_PASSWORD=yourpasswqord

Create Migration and Tables

php artisan make:migration create_supplier_table

Create tables

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSupplierTable extends Migration {

    public function up(){
        // create supplier table
        Schema::create('supplier', function (Blueprint $table) {
         $table->increments('id');
         $table->string('supplierName')->unique();
         $table->string('supplierEmail')->unique();
         $table->string('supplierContact');
         $table->string('supplierPosition');
         $table->timestamps();
        });
    }

    public function down() {
        // Drop supplier table
        Scheme::drop('supplier');
    }
}

Run Migration

php artisan migrate

Create Model

php artisan make:model Supplier

Supplier.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Supplier extends Model{
    protected $table ='supplier';
    protected $fillable = array('id','supplierName','supplierEmail','supplierContact','supplierPosition');
}

Create Controller

php artisan make:controller SupplierController --resource

SupplierController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Supplier; // use our supplier model

class SupplierController extends Controller {

    public function index($id = null) {
      if ($id == null){
        return Supplier::orderBy('id', 'asc')->get();
      } else {
        return $this->show($id);
      }
    }

    public function store(Request $request) {
        $supplier = new Supplier;
        $supplier->supplierName = $request->input('supplierName');
        $supplier->supplierEmail = $request->input('supplierEmail');
        $supplier->supplierContact = $request->input('supplierContact');
        $supplier->supplierPosition = $request->input('supplierPosition');
        $supplier->save();
        return 'Supplier record successfully created with id' . $supplier->id;
    }
    public function show($id) {
        return Supplier::find($id);
    }
    public function update(Request $request, $id) {
        $supplier = Supplier::find($id);
        $supplier->supplierName = $request->input('supplierName');
        $supplier->supplierEmail = $request->input('supplierEmail');
        $supplier->supplierContact = $request->input('supplierContact');
        $supplier->supplierPosition = $request->input('supplierPosition');
        $supplier->save();
        return 'Supplier record successfully updated with id ' . $supplier->id;
    }
    public function destroy($id) {
        $supplier = Supplier::find($id)->delete();
        return 'Employee record successfully deleted';
    }
}

Create New Routes

Route::get('/', function () {
    return view('index');
});
Route::get('/api/supplier/{id?}', 'SupplierController@index');
Route::post('/api/supplier', 'SupplierController@store');
Route::post('/api/supplier/{id}', 'SupplierController@update');
Route::delete('/api/supplier/{id}', 'SupplierController@destroy');

Create New Views (index.blade.php)

<!DOCTYPE html>
<html lang="en" ng-app="getSupplier">
  <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>Laravel 5 and Angular CRUD Application</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/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>

    <div class="container">
      <h2>Simple CRUD Application with AngularJs</h2>
      <div ng-controller="SupplierController">
        <table class="table table-striped">
          <thead>
            <tr>
              <th>ID</th>
              <th>Supplier Name</th>
              <th>Supplier Email</th>
              <th>Supplier Contact</th>
              <th>Supplier Position</th>
              <th>
                <button id="btn-add" class="btn btn-success btn-xs" ng-click="toggle('add', 0)">Add New Supplier</button>
              </th>
            </tr>
          </thead>
          <tbody>
            <tr ng-repeat="supplier in suppliers">
              <td>@{{ supplier.id }}</td>
              <td>@{{ supplier.supplierName }}</td>
              <td>@{{ supplier.supplierEmail }}</td>
              <td>@{{ supplier.supplierContact }}</td>
              <td>@{{ supplier.supplierPosition }}</td>
              <td>
                <button class="btn btn-warning btn-xs btn-detail" ng-click="toggle('edit', supplier.id)">
                  <span class="glyphicon glyphicon-edit"></span>
                </button>
                <button class="btn btn-danger btn-xs btn-delete" ng-click="confirmDelete(supplier.id)">
                  <span class="glyphicon glyphicon-trash"></span>
                </button>
              </td>
            </tr>
          </tbody>
        </table>
        <!-- show modal  -->
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">@{{form_title}}</h4>
              </div>
              <div class="modal-body">
                <form name="frmSupplier" class="form-horizontal" novalidate="">
                  <div class="form-group">
                    <label class="col-sm-3 control-label">Supplier Name</label>
                    <div class="col-sm-9">
                      <input type="text" class="form-control" id="supplierName" name="supplierName" placeholder="Supplier Name" value="@{{supplierName}}" ng-model="supplier.supplierName" ng-required="true">
                      <span ng-show="frmSupplier.supplierName.$invalid && frmSupplier.supplierName.$touched">Supplier Name field is required</span>
                    </div>
                  </div>

                    <div class="form-group">
                      <label class="col-sm-3 control-label">Supplier Email</label>
                      <div class="col-sm-9">
                        <input type="email" class="form-control" id="supplierEmail" name="supplierEmail" placeholder="Supplier Email" value="@{{supplierEmail}}" ng-model="supplier.supplierEmail" ng-required="true">
                        <span ng-show="frmSupplier.supplierEmail.$invalid && frmSupplier.supplierEmail.$touched">Supplier Email field is required</span>
                      </div>
                    </div>

                    <div class="form-group">
                      <label class="col-sm-3 control-label">Supplier Contact</label>
                      <div class="col-sm-9">
                        <input type="text" class="form-control" id="supplierContact" name="supplierContact" placeholder="Supplier Contact" value="@{{supplierContact}}" ng-model="supplier.supplierContact" ng-required="true">
                        <span ng-show="frmSupplier.supplierContact.$invalid && frmSupplier.supplierContact.$touched">Supplier Contact field is required</span>
                      </div>
                    </div>

                    <div class="form-group">
                      <label class="col-sm-3 control-label">Supplier Position</label>
                      <div class="col-sm-9">
                        <input type="text" class="form-control" id="supplierPosition" name="supplierPosition" placeholder="Supplier Position" value="@{{supplierPosition}}" ng-model="supplier.supplierPosition" ng-required="true">
                        <span ng-show="frmSupplier.supplierPosition.$invalid && frmSupplier.supplierPosition.$touched">Supplier Position field is required</span>
                      </div>
                    </div>
                </form>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="btn-save" ng-click="save(modalstate, id)" ng-disabled="frmSupplier.$invalid">Save Changes</button>
              </div>
            </div>
          </div>
        </div>
      </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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

    <!-- Aangular Material load from CDN -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.1/angular-material.min.js"></script>

    <!-- Angular Application Scripts Load  -->
    <script src="{{ asset('angular/app.js') }}"></script>
    <script src="{{ asset('angular/controllers/SupplierController.js') }}"></script>
  </body>
</html>

AngularJS application structure

Laravel 5 Tutorial : Laravel 5.3 Simple CRUD Application With Angular JS

Create new folder (angular) in public\angular, and create new file (app.js) in there (public\angular\app.js)

public\angular\app.js

var app = angular.module('getSupplier', [])
        .constant('API_URL','http://localhost:8080/api/');

Create new folder (controller) in public\angular directory, and create new file (SupplierController.js) in public\angular\controller\SupplierController.js folder

public\angular\controller\SupplierController.js

app.controller('SupplierController', function($scope, $http, API_URL) {
  // retrieve Supplier listing from API
  $http.get(API_URL + "supplier")
  .success(function(response){
    $scope.suppliers = response;
  });

  // show modal Form
  $scope.toggle = function(modalstate, id) {
      $scope.modalstate = modalstate;
      switch(modalstate) {
        case 'add':
          $scope.form_title = "Add New Supplier";
          break;
        case 'edit':
          $scope.form_title = "Supplier Detail";
          $scope.id = id;
          $http.get(API_URL + 'supplier/' + id).success(function(response){
            console.log(response);
            $scope.supplier = response;
          });
          break;
        default:
          break;
      }
      console.log(id);
      $('#myModal').modal('show');
  }

  // save new supplier and update existing supplier
  $scope.save = function(modalstate, id) {
    var url = API_URL + "supplier";
    if (modalstate === 'edit') {
      url += "/" + id;
    }
    $http({
      method: 'POST',
      url: url,
      data: $.param($scope.supplier),
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function(response){
      console.log(response);
      location.reload();
    }).error(function(response){
      console.log(response);
      alert('This is embarassing. An error has occured. Please check the log for details');
    });
  }

 // delete supplier record
 $scope.confirmDelete = function(id) {
   var isConfirmDelete = confirm('Are you sure you want this record?');
   if (isConfirmDelete) {
     $http({
       method: 'DELETE',
       url: API_URL + 'supplier/' + id
     }).success(function(data){
       console.log(data);
       location.reload();
     }).error(function(data){
       console.log(data);
       alert('Unable to delete');
     });
   } else {
     return false;
   }
 }
});

Watch more Laravel videos

Laravel 5.3 CRUD with VueJS



Laravel 5.3 CRUD with AJAX


DOWNLOAD FULL SOURCE CODE Laravel 5.3 Simple CRUD Application With Angular JS HERE!!!.

See you next lessons ..

Thứ Hai, 5 tháng 12, 2016

Laravel 5 Tutorial : Create a custom Laravel5.3 Pagination with Bootstrap Templates


Laravel 5.3 Tutorial - How to Create a custom Laravel 5.3 Pagination with Bootstrap Templates step by step for beginners? this lessons will show you how to create pagination in laravel 5.3 and customizing it with bootstrap templates.

At the previews lessons we have create simple Pagination with Eloquent & Query Builder in Laravel 5.3, so please read it before.

Video Tutorial Customizing Bootstrap Pagination Templates

Watch this video tutorial and download all source code above.


Full source code Pagination with bootstrap in Laravel 5.3

Pagination.blade.php (resources\views\admin\posts\pagination.blade.php)

@if ($paginator->hasPages())
  <ul class="pager">
    <!-- Previous Link Page -->
    @if ($paginator->onFirstPage())
      <li class="disabled">
        <span class="glyphicon glyphicon-menu-left" aria-hidden="true"> Previews</span>
      </li>
    @else
      <li>
        <a href="{{ $paginator->previousPageUrl() }}" rel="prev">
        <span class="glyphicon glyphicon-menu-left" aria-hidden="true"> Previews</span></a>
      </li>
    @endif

    <!-- Pagination Elements -->
    @foreach ($elements as $element)
      @if (is_string($element))
        <li class="disabled">
          <span>{{ $element }}</span>
        </li>
      @endif
      <!-- Array Of Links -->
      @if(is_array($element))
        @foreach($element as $page => $url)
          @if ($page == $paginator->currentPage())
            <li clas="active my-active">
              <span>{{ $page }}</span>
            </li>
          @else
            <li>
              <a href="{{ $url }}">{{ $page }}</a>
            </li>
          @endif
        @endforeach
      @endif
    @endforeach
    <!-- Next Link Page -->
    @if($paginator->hasMorePages())
      <li>
        <a href="{{ $paginator->nextPageUrl() }}" rel="next">
        Next <span class="glyphicon glyphicon-menu-right" aria-hidden="true"></span></a>
      </li>
    @else
      <li class="disabled">
        Next <span class="glyphicon glyphicon-menu-right" aria-hidden="true"></span>
      </li>
    @endif
  </ul>
@endif

Next, we need to edit our post page (resources\views\admin\posts\allposts.blade.php)

Allposts.blade.php

{!! $posts->links('admin/posts/pagination') !!}

More laravel tutorial



See you next lessons ...

Laravel 5 Tutorial : Create a custom Laravel5.3 Pagination with Bootstrap Templates


Laravel 5.3 Tutorial - How to Create a custom Laravel 5.3 Pagination with Bootstrap Templates step by step for beginners? this lessons will show you how to create pagination in laravel 5.3 and customizing it with bootstrap templates.

At the previews lessons we have create simple Pagination with Eloquent & Query Builder in Laravel 5.3, so please read it before.

Video Tutorial Customizing Bootstrap Pagination Templates

Watch this video tutorial and download all source code above.


Full source code Pagination with bootstrap in Laravel 5.3

Pagination.blade.php (resources\views\admin\posts\pagination.blade.php)

@if ($paginator->hasPages())
  <ul class="pager">
    <!-- Previous Link Page -->
    @if ($paginator->onFirstPage())
      <li class="disabled">
        <span class="glyphicon glyphicon-menu-left" aria-hidden="true"> Previews</span>
      </li>
    @else
      <li>
        <a href="{{ $paginator->previousPageUrl() }}" rel="prev">
        <span class="glyphicon glyphicon-menu-left" aria-hidden="true"> Previews</span></a>
      </li>
    @endif

    <!-- Pagination Elements -->
    @foreach ($elements as $element)
      @if (is_string($element))
        <li class="disabled">
          <span>{{ $element }}</span>
        </li>
      @endif
      <!-- Array Of Links -->
      @if(is_array($element))
        @foreach($element as $page => $url)
          @if ($page == $paginator->currentPage())
            <li clas="active my-active">
              <span>{{ $page }}</span>
            </li>
          @else
            <li>
              <a href="{{ $url }}">{{ $page }}</a>
            </li>
          @endif
        @endforeach
      @endif
    @endforeach
    <!-- Next Link Page -->
    @if($paginator->hasMorePages())
      <li>
        <a href="{{ $paginator->nextPageUrl() }}" rel="next">
        Next <span class="glyphicon glyphicon-menu-right" aria-hidden="true"></span></a>
      </li>
    @else
      <li class="disabled">
        Next <span class="glyphicon glyphicon-menu-right" aria-hidden="true"></span>
      </li>
    @endif
  </ul>
@endif

Next, we need to edit our post page (resources\views\admin\posts\allposts.blade.php)

Allposts.blade.php

{!! $posts->links('admin/posts/pagination') !!}

More laravel tutorial



See you next lessons ...

Thứ Bảy, 3 tháng 12, 2016

Laravel 5 & JQuery Tutorial : Bootstrap-validator Validation Example in Laravel 5.3


Laravel 5.3 tutorial for beginners - How to create validation using JQuery Bootstrap-validator in Laravel 5.3? this tutorial will show you how to build simple validation with Bootstrap-validator plugins the best plugin for Form validation.

At the previews lessons we have learn how to create simple validation in laravel, please read How to use Validation in Laravel 5.3.

Video tutorial Using Bootstrap-validator in laravel 5.3


Full Source Code create Form Validation in Laravel 5.3

New Route (routes\web.php)

  // new route for simple validation page
  Route::get('/validation',function(){
    return view('validation');
  });

New View (resources\view\validation.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>Simple Validation Form</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/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>
    <div class="container">
      <div class="row">
        {!! Form::open(array('method'=>'POST','files'=>true,'id'=>'validateForm')) !!}
        <div class="col-sm-12">
          <h2>Simple Validation Form</h2>
        </div>
        <div class="col-xs-8">
          <div class="form-group">
            <strong>User Name :</strong>
            <input type="text" name="username" class="form-control">
          </div>
        </div>

        <div class="col-xs-8">
          <div class="form-group">
            <strong>Email :</strong>
            {!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}
            {!! $errors->first('email', '<p class="alert alert-danger">:message</p>') !!}
          </div>
        </div>

        <div class="col-xs-8">
          <div class="form-group">
            <strong>Password :</strong>
            <input type="password" name="password" class="form-control">
          </div>
        </div>

        <div class="col-xs-8">
          <div class="form-group">
            <strong>Phone Number :</strong>
            {!! Form::text('phone', null, array('placeholder' => 'Mobila Number','class' => 'form-control')) !!}
            {!! $errors->first('phone', '<p class="alert alert-danger">:message</p>') !!}
          </div>
        </div>

        <div class="col-xs-8">
          <div class="form-group">
            <strong>Gender</strong>
            <div class="radio">
              <label>
                <input type="radio" name="gender" value="male"> Male
              </label>
            </div>
            <div class="radio">
              <label>
                <input type="radio" name="gender" value="female"> Female
              </label>
            </div>
            <div class="radio">
              <label>
                <input type="radio" name="gender" value="other"> Other
              </label>
            </div>
          </div>
        </div>
        <div class="col-xs-8">
          <button type="submit" class="btn btn-success">Submit</button>
          <button type="submit" class="btn btn-warning">Clear</button>
          <button type="submit" class="btn btn-primary">Back</button>
        </div>
      </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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<!-- java scrip to validate the form. -->
  <script type="text/javascript">
    $(document).ready(function(){
      $('#validateForm').bootstrapValidator({
        message:'This value is not valid',
        feedbackIcons: {
          valid: 'glyphicon glyphicon-ok',
          invalid: 'glyphicon glyphicon-remove',
          validating: 'glyphicon glyphicon-refresh'
      },
      fields: {
        username: {
          validators: {
            notEmpty: {
              message: 'The username is required'
            },
            stringLength: {
              min: 6,
              max: 20,
              message:'The username must be more than 6 and less than 30 characters long'
            },
            regexp: {
              regexp: /^[a-zA-Z0-9_\.]+$/,
              message: 'The username can only consist of alphabetical, number, dot and underscore'
            },
          }
        },

        password: {
          validators: {
              notEmpty: {
                  message: 'The password is required'
              },
              different: {
                  field: 'username',
                  message: 'The password cannot be the same as username'
              }
          }
      },

      gender: {
        validators: {
            notEmpty: {
                message: 'The gender is required'
            }
        }
    },

      email: {
        validators: {
            notEmpty: {
                message: 'Please enter your email'
            }, emailAddress: {
                message: 'The value is not a valid email address'
            }
        }
    },

      phone: {
        validators: {
        notEmpty: {
                message: 'Please enter your phone no.'
            },
        digits: {
                message: 'The mobile phone number is not valid'
            },
            stringLength: {
                min: 10,
                max: 12,
                message: 'The phone no. must be 10 digits'
            }
        }
    },
  }
});
});
</script>
    <!-- add library to validate form  -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-validator/0.5.3/js/bootstrapValidator.min.js"></script>
  </body>
</html>

Watch more Laravel Video Tutorial's



See you next lessons

Laravel 5 & JQuery Tutorial : Bootstrap-validator Validation Example in Laravel 5.3


Laravel 5.3 tutorial for beginners - How to create validation using JQuery Bootstrap-validator in Laravel 5.3? this tutorial will show you how to build simple validation with Bootstrap-validator plugins the best plugin for Form validation.

At the previews lessons we have learn how to create simple validation in laravel, please read How to use Validation in Laravel 5.3.

Video tutorial Using Bootstrap-validator in laravel 5.3


Full Source Code create Form Validation in Laravel 5.3

New Route (routes\web.php)

  // new route for simple validation page
  Route::get('/validation',function(){
    return view('validation');
  });

New View (resources\view\validation.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>Simple Validation Form</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/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>
    <div class="container">
      <div class="row">
        {!! Form::open(array('method'=>'POST','files'=>true,'id'=>'validateForm')) !!}
        <div class="col-sm-12">
          <h2>Simple Validation Form</h2>
        </div>
        <div class="col-xs-8">
          <div class="form-group">
            <strong>User Name :</strong>
            <input type="text" name="username" class="form-control">
          </div>
        </div>

        <div class="col-xs-8">
          <div class="form-group">
            <strong>Email :</strong>
            {!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}
            {!! $errors->first('email', '<p class="alert alert-danger">:message</p>') !!}
          </div>
        </div>

        <div class="col-xs-8">
          <div class="form-group">
            <strong>Password :</strong>
            <input type="password" name="password" class="form-control">
          </div>
        </div>

        <div class="col-xs-8">
          <div class="form-group">
            <strong>Phone Number :</strong>
            {!! Form::text('phone', null, array('placeholder' => 'Mobila Number','class' => 'form-control')) !!}
            {!! $errors->first('phone', '<p class="alert alert-danger">:message</p>') !!}
          </div>
        </div>

        <div class="col-xs-8">
          <div class="form-group">
            <strong>Gender</strong>
            <div class="radio">
              <label>
                <input type="radio" name="gender" value="male"> Male
              </label>
            </div>
            <div class="radio">
              <label>
                <input type="radio" name="gender" value="female"> Female
              </label>
            </div>
            <div class="radio">
              <label>
                <input type="radio" name="gender" value="other"> Other
              </label>
            </div>
          </div>
        </div>
        <div class="col-xs-8">
          <button type="submit" class="btn btn-success">Submit</button>
          <button type="submit" class="btn btn-warning">Clear</button>
          <button type="submit" class="btn btn-primary">Back</button>
        </div>
      </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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<!-- java scrip to validate the form. -->
  <script type="text/javascript">
    $(document).ready(function(){
      $('#validateForm').bootstrapValidator({
        message:'This value is not valid',
        feedbackIcons: {
          valid: 'glyphicon glyphicon-ok',
          invalid: 'glyphicon glyphicon-remove',
          validating: 'glyphicon glyphicon-refresh'
      },
      fields: {
        username: {
          validators: {
            notEmpty: {
              message: 'The username is required'
            },
            stringLength: {
              min: 6,
              max: 20,
              message:'The username must be more than 6 and less than 30 characters long'
            },
            regexp: {
              regexp: /^[a-zA-Z0-9_\.]+$/,
              message: 'The username can only consist of alphabetical, number, dot and underscore'
            },
          }
        },

        password: {
          validators: {
              notEmpty: {
                  message: 'The password is required'
              },
              different: {
                  field: 'username',
                  message: 'The password cannot be the same as username'
              }
          }
      },

      gender: {
        validators: {
            notEmpty: {
                message: 'The gender is required'
            }
        }
    },

      email: {
        validators: {
            notEmpty: {
                message: 'Please enter your email'
            }, emailAddress: {
                message: 'The value is not a valid email address'
            }
        }
    },

      phone: {
        validators: {
        notEmpty: {
                message: 'Please enter your phone no.'
            },
        digits: {
                message: 'The mobile phone number is not valid'
            },
            stringLength: {
                min: 10,
                max: 12,
                message: 'The phone no. must be 10 digits'
            }
        }
    },
  }
});
});
</script>
    <!-- add library to validate form  -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-validator/0.5.3/js/bootstrapValidator.min.js"></script>
  </body>
</html>

Watch more Laravel Video Tutorial's



See you next lessons

Thứ Sáu, 2 tháng 12, 2016

Laravel Scout and Vue.Js : How to create Search Function in Laravel 5.3


Laravel 5.3 tutorial : This lesson will show you how to create simple search function using Laravel Scout and Vue.Js, at the previews lessons we have learn how to build simple search using GET Method, please read Simple Search Function Using GET Method in laravel 5.3.

How to create Search Function in Laravel 5.3?

First, we will need laravel was installed on our server, if not, i assumed you to read and do step by step on this lessons How to build Blog using laravel step by step.

Video tutorial How to create Search Function in Laravel 5.3

Just watch this video, and follow step by step.


Routes (Web and Api)

web.php

Route::get('/','Api\SearchController@search');

api.php

Route::get('/search', 'Api\SearchController@search');

Api Controller (SearchController.php)

<?php
namespace App\Http\Controllers\Api;
use App\Posts;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class SearchController extends Controller {
    // we will Installing and configuring Laravel Scout
    public function search(Request $req){
      // First we define the error message we are going to show if no keywords
      $error = ['error'=>'No results found'];
      // if the user entered the keyword
      if ($req->has('q')){
        // Using the Laravel Scout syntax to search the products table.
        $posts = Posts::search($req->get('q'))->get();
        // If there are results return them, if none, return the error message.
        return $posts->count() ? $posts : $error;
      } else {
        // we will show all posts data from database
        $posts = Posts::all();
        return view('search')->withPosts($posts);
      }
    }
}

Search.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>Simple Vue.Js Search Function</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/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>

    <div class="container">
      <div class="row" id="posts">
        <div class="col-md-12">
          <div class="input-group input-group-sm">
            <div class="icon-addon addon-md">
              <input type="text" v-model="query" placeholder="What are you looking for?" class="form-control">
            </div>
            <span class="input-group-btn">
              <button type="button" class="btn-sm btn-danger" v-if="!loading" @click="search()">
                Search <i class="fa fa-search"></i>
              </button>
              <button type="button" class="btn-sm btn-danger" v-if="loading" disabled="disabled">
                Searching... <i class="fa fa-search"></i>
              </button>
            </span>
          </div>
        </div>

        <div class="row">
          <div class="col-md-12">
            <div class="post-preview" v-for="post in posts">
              <p>
                <span class="well-sm"><strong>
                  <a href="#">@{{ post.title }}</a>
                </strong>
                </span>
                <span class="alert-danger">
                  On @{{ post.created_at }}
                </span>
              </p>
            </div>
          </div>
        </div>
      </div>
      <!-- Show all data posts from database -->
      <div class="row">
        <div class="col-md-12">
          <div class="post-preview">
            <div class="alert alert-warning" v-if="noresult">
              <h2><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                @{{ noresult }}
              </h2>
              <span class="well-sm" v-if="noresult">
                @foreach($posts as $post)
                  <p>
                    <strong><a href="#">{{ $post->description }}</a></strong>
                    <span class="alert-danger">On {{ $post->created_at->format('M d,Y \a\t h:i a') }}</span>
                  </p>
                @endforeach
              </span>
            </div>
          </div>
        </div>
      </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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue-resource/1.0.1/vue-resource.min.js"></script>
    <script src="/js/search.js"></script>
  </body>
</html>

search.js

new Vue({
    el: 'body',
    data: {
      posts: [],
      loading: false,
      noresult: 'Showing All Posts',
      query: ''
  },
  methods: {
    search: function() {
        // Clear the error message.
        this.noresult = '';
        // Empty the posts array so we can fill it with the new posts.
        this.posts = [];
        // Set the loading property to true, this will display the "Searching..." button.
        this.loading = true;

        // Making a get request to our API and passing the query to it.
        this.$http.get('/api/search?q=' + this.query).then((response) => {
            // If there was an error set the error message, if not fill the posts array.
            response.body.error ? this.noresult = response.body.error : this.posts = response.body;
            // The request is finished, change the loading to false again.
            this.loading = false;
            // Clear the query.
            this.query = '';
        });
    }
  }
});

More Vue.Js & Laravel Video Tutorial :



Full source code laravel app with vue.js
see you next lessons.