Laravel PHP Framework and AngularJS Search and Pagination with CRUD Example

Laravel PHP Framework and AngularJS Search and Pagination with CRUD  Example

Search and Pagination with CRUD  Example in Laravel PHP Framework and AngularJS 

In my previous post, you learn how you can create a simple CRUD(Create Read Update Delete) application with Laravel 5.2 , i hope after following steps you will create your first admin panel.

Now I will tell you that how to create application with search , pagination and CRUD in Laravel 5.2 and AngularJS, kindly follow each step to create your first web application having modules with create, edit, delete, list, search and pagination functionality. Using AngularJS it is much more easier to work with events and workin with Laravel AngularJs then it make your application awesome. You must have knowledge about basic AngularJS before goint to create CRUD Application in Laravel AngularJS.

Step 1: Create Product Table and Module

In First step, create migration file for product table using Laravel 5 php artisan command.Run following command :

php artisan make:migration create_products_table

After this command, you will see a migration file in following path database/migrations and you have to simply put following code in migration file to create products table.


  1. use Illuminate\Database\Schema\Blueprint;
  2. use Illuminate\Database\Migrations\Migration;
  3. class CreateProductsTable extends Migration
  4. {
  5. public function up()
  6. {
  7. Schema::create('products', function (Blueprint $table) {
  8. $table->increments('id');
  9. $table->string('name');
  10. $table->text('details');
  11. $table->timestamps();
  12. });
  13. }
  14. public function down()
  15. {
  16. Schema::drop("products");
  17. }
  18. }

Save this migration file and run following command

php artisan migrate

After create `products` table, yor should create model for product table.Create file in following path app/Product.php and put bellow couple of code in Product.php file:

app/Product.php

  1. namespace App;
  2. use Illuminate\Database\Eloquent\Model;
  3. class Product extends Model
  4. {
  5. public $fillable = ['name','details'];
  6. }
Step2: Create Product Controller

Now we will create ProductController.php in following path app/Http/Controllers, all request(lists, create, edit, delete ,search and pagination) will manage by this ProductCRUDController.php file.

app/Http/Controllers/ProductController.php

  1. namespace App\Http\Controllers;
  2. use Illuminate\Http\Request;
  3. use App\Http\Requests;
  4. use App\Http\Controllers\Controller;
  5. use App\Product;
  6. class ProductController extends Controller
  7. {
  8. /**
  9. * Display a listing of the resource.
  10. *
  11. * @return Response
  12. */
  13. public function index(Request $request)
  14. {
  15. $input = $request->all();
  16. $products=Product::query();
  17. if($request->get('search')){
  18. $products = $products->where("name", "LIKE", "%{$request->get('search')}%");
  19. }
  20.          $products = $products->paginate(5);
  21. return response($products);
  22. }
  23. /**
  24. * Store a newly created resource in storage.
  25. *
  26. * @return Response
  27. */
  28. public function store(Request $request)
  29. {
  30.     $input = $request->all();
  31. $create = Product::create($input);
  32. return response($create);
  33. }
  34. /**
  35. * Show the form for editing the specified resource.
  36. *
  37. * @param int $id
  38. * @return Response
  39. */
  40. public function edit($id)
  41. {
  42. $product = Product::find($id);
  43. return response($product);
  44. }
  45. /**
  46. * Update the specified resource in storage.
  47. *
  48. * @param int $id
  49. * @return Response
  50. */
  51. public function update(Request $request,$id)
  52. {
  53.     $input = $request->all();
  54. $product=Product::find($id);
  55. $product->update($input);
  56. $product=Product::find($id);
  57. return response($product);
  58. }
  59. /**
  60. * Remove the specified resource from storage.
  61. *
  62. * @param int $id
  63. * @return Response
  64. */
  65. public function destroy($id)
  66. {
  67. return Product::where('id',$id)->delete();
  68. }
  69. }
Step3: Route File

Now we will add some route in routes.php. First add resource route for products module and another route for template route. using template route we will get html template for our app. So let's add below content in route file :

app/Http/routes.php

  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Routes File
  5. |--------------------------------------------------------------------------
  6. |
  7. | Here is where you will register all of the routes in an application.
  8. | It's a breeze. Simply tell Laravel the URIs it should respond to
  9. | and give it the controller to call when that URI is requested.
  10. |
  11. */
  12. Route::get('/', function () {
  13. return view('app');
  14. });
  15. /*
  16. |--------------------------------------------------------------------------
  17. | Application Routes
  18. |--------------------------------------------------------------------------
  19. |
  20. | This route group applies the "web" middleware group to every route
  21. | it contains. The "web" middleware group is defined in your HTTP
  22. | kernel and includes session state, CSRF protection, and more.
  23. |
  24. */
  25. Route::group(['middleware' => ['web']], function () {
  26. Route::resource('products', 'ProductController');
  27. });
  28. // Angular HTML Templates
  29. Route::group(array('prefix'=>'/htmltemplates/'),function(){
  30. Route::get('{htmltemplates}', array( function($htmltemplates)
  31. {
  32. $htmltemplates = str_replace(".html","",$htmltemplates);
  33. View::addExtension('html','php');
  34. return View::make('htmltemplates.'.$htmltemplates);
  35. }));
  36. });
Step4: Manage AngularJS route and controller

First we will create a separate directory with name `app` in public directory public/app where we will put all angularjs files so that it will be easier to manage files

Create route.js file in following path public/app/route.js

route.js

  1. var app = angular.module('main-App',['ngRoute','angularUtils.directives.dirPagination']);
  2. app.config(['$routeProvider',
  3. function($routeProvider) {
  4. $routeProvider.
  5. when('/', {
  6. templateUrl: 'htmltemplates/home.html',
  7. controller: 'AdminController'
  8. }).
  9. when('/products', {
  10. templateUrl: 'htmltemplates/products.html',
  11. controller: 'ProductController'
  12. });
  13. }]);
  14. app.value('apiUrl', 'public path url');

app.value define global variable which will be injected in controller where we use this variable

Now we will create a controller directory in app directory with name `controllers` and create ProductController.js file in following path public/app/controllers/ProductController.js

ProductController.js

  1. app.controller('AdminController', function($scope,$http){
  2. $scope.pools = [];
  3. });
  4. app.controller('ProductController', function(dataFactory,$scope,$http,apiUrl){
  5. $scope.data = [];
  6. $scope.libraryTemp = {};
  7. $scope.totalProductsTemp = {};
  8. $scope.totalProducts = 0;
  9. $scope.pageChanged = function(newPage) {
  10. getResultsPage(newPage);
  11. };
  12. getResultsPage(1);
  13. function getResultsPage(pageNumber) {
  14. if(! $.isEmptyObject($scope.libraryTemp)){
  15. dataFactory.httpRequest(apiUrl+'/products?search='+$scope.searchInputText+'&page='+pageNumber).then(function(data) {
  16. $scope.data = data.data;
  17. $scope.totalProducts = data.total;
  18. });
  19. }else{
  20. dataFactory.httpRequest(apiUrl+'/products?page='+pageNumber).then(function(data) {
  21. console.log(data);
  22. $scope.data = data.data;
  23. $scope.totalProducts = data.total;
  24. });
  25. }
  26. }
  27. $scope.dbSearch = function(){
  28. if($scope.searchInputText.length >= 3){
  29. if($.isEmptyObject($scope.libraryTemp)){
  30. $scope.libraryTemp = $scope.data;
  31. $scope.totalProductsTemp = $scope.totalProducts;
  32. $scope.data = {};
  33. }
  34. getResultsPage(1);
  35. }else{
  36. if(! $.isEmptyObject($scope.libraryTemp)){
  37. $scope.data = $scope.libraryTemp ;
  38. $scope.totalProducts = $scope.totalProductsTemp;
  39. $scope.libraryTemp = {};
  40. }
  41. }
  42. }
  43. $scope.saveAdd = function(){
  44. dataFactory.httpRequest('products','POST',{},$scope.form).then(function(data) {
  45. $scope.data.push(data);
  46. $(".modal").modal("hide");
  47. });
  48. }
  49. $scope.edit = function(id){
  50. dataFactory.httpRequest(apiUrl+'/products/'+id+'/edit').then(function(data) {
  51.     console.log(data);
  52.     $scope.form = data;
  53. });
  54. }
  55. $scope.updateProduct = function(){
  56. dataFactory.httpRequest(apiUrl+'/products/'+$scope.form.id,'PUT',{},$scope.form).then(function(data) {
  57.     $(".modal").modal("hide");
  58. $scope.data = apiModifyTable($scope.data,data.id,data);
  59. });
  60. }
  61. $scope.remove = function(product,index){
  62. var result = confirm("Are you sure delete this product?");
  63.     if (result) {
  64. dataFactory.httpRequest(apiUrl+'/products/'+product.id,'DELETE').then(function(data) {
  65. $scope.data.splice(index,1);
  66. });
  67. }
  68. }
  69. });

Now we will create a folder with name `myhelper` in app directory for helper.js file, which will help to define helper functions.

helper.js

  1. function apiModifyTable(mainData,id,response){
  2. angular.forEach(mainData, function (product,key) {
  3. if(product.id == id){
  4. mainData[key] = response;
  5. }
  6. });
  7. return mainData;
  8. }

Now we will create another directory `packages` and create dirPagination.js file in following path public/app/packages/dirPagination.jsand put code of following link :

dirPagination.js

Now We will create another directory `services` and create services.js file in following path public/app/myservices/services.js and put code of following link :

services.js

Step5: Create View

This is final step, where you have to create view files. So first start to create angularapp.blade.php in following path resources/views/angularapp.blade.php and put following code :

angularapp.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1">
  7.     <title>Laravel 5.2</title>
  8.     <!-- Fonts -->
  9.     <link href='//fonts.googleapis.com/css?family=Roboto:400,300' rel='stylesheet' type='text/css'>
  10.     
  11.     <link rel="stylesheet" href="http://www.expertphp.in/css/bootstrap.css">
  12.     <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  13.     <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>
  14.     
  15.     <!-- Angular JS -->
  16.     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
  17.     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-route.min.js"></script>
  18.     <!-- MY App -->
  19.     <script src="{{ asset('app/packages/dirPagination.js') }}"></script>
  20.     <script src="{{ asset('app/routes.js') }}"></script>
  21.     <script src="{{ asset('app/myservices/services.js') }}"></script>
  22.     <script src="{{ asset('app/myhelper/helper.js') }}"></script>
  23.     <!-- App Controller -->
  24.     <script src="{{ asset('/app/controllers/ProductController.js') }}"></script>
  25.     
  26. </head>
  27. <body ng-app="main-App">
  28.     <nav class="navbar navbar-default">
  29.         <div class="container-fluid">
  30.             <div class="navbar-header">
  31.                 <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#collapse-1-example">
  32.                     <span class="sr-only">Toggle Navigation</span>
  33.                     <span class="icon-bar"></span>
  34.                     <span class="icon-bar"></span>
  35.                     <span class="icon-bar"></span>
  36.                 </button>
  37.                 <a class="navbar-brand" href="#">Laravel 5.2</a>
  38.             </div>
  39.             <div class="collapse navbar-collapse" id="collapse-1-example">
  40.                 <ul class="nav navbar-nav">
  41.                     <li><a href="#/">Home</a></li>
  42.                     <li><a href="#/products">Product</a></li>
  43.                 </ul>
  44.             </div>
  45.         </div>
  46.     </nav>
  47.     <div id="container_area" class="container">
  48.         <ng-view></ng-view>
  49.     </div>
  50.     <!-- Scripts -->
  51. </body>
  52. </html>

Now we will create htmltemplates folder in following path resources/views/ and then create home.html file in htmltemplates folder.

resources/views/htmltemplates/home.html

  1. <h2>Welcome to Landing Page</h2>

resources/views/htmltemplates/products.html


  1. <div class="row">
  2. <div class="col-lg-12 margin-tb">
  3. <div class="pull-left">
  4. <h1>Product Management</h1>
  5. </div>
  6. <div class="pull-right" style="padding-top:30px">
  7. <div class="box-tools" style="display:inline-table">
  8. <div class="input-group">
  9. <input type="text" class="form-control input-sm ng-valid ng-dirty" placeholder="Search" ng-change="dbSearch()" ng-model="searchInputText" name="table_search" title="" tooltip="" data-original-title="Minimum length of character is 3">
  10. <span class="input-group-addon">Search</span>
  11. </div>
  12. </div>
  13. <button class="btn btn-success" data-toggle="modal" data-target="#add-new-product">Create New Product</button>
  14. </div>
  15. </div>
  16. </div>
  17. <table class="table table-bordered pagin-table">
  18. <thead>
  19. <tr>
  20. <th>No</th>
  21. <th>Name</th>
  22. <th>Details</th>
  23. <th width="220px">Action</th>
  24. </tr>
  25. </thead>
  26. <tbody>
  27. <tr dir-paginate="value in data | productsPerPage:5" total-products="totalProducts">
  28. <td>{{ $index + 1 }}</td>
  29. <td>{{ value.name }}</td>
  30. <td>{{ value.details }}</td>
  31. <td>
  32. <button data-toggle="modal" ng-click="edit(value.id)" data-target="#edit-data" class="btn btn-primary">Edit</button>
  33. <button ng-click="remove(value,$index)" class="btn btn-danger">Delete</button>
  34. </td>
  35. </tr>
  36. </tbody>
  37. </table>
  38. <dir-pagination-controls class="pull-right" on-page-change="pageChanged(newPageNumber)" template-url="htmltemplates/dirPagination.html" ></dir-pagination-controls>
  39. <!-- Create Modal -->
  40. <div class="modal" id="add-new-product" tabindex="-1" role="dialog" aria-labelledby="myModalForLabel">
  41. <div class="modal-dialog" role="document">
  42. <div class="modal-content">
  43. <form method="POST" name="addProduct" role="form" ng-submit="saveAdd()">
  44. <input type="hidden" name="_token" value="{!! csrf_token() !!}">
  45. <div class="modal-header">
  46. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  47. <h4 class="modal-title" id="myModalForLabel">Create Product</h4>
  48. </div>
  49. <div class="modal-body">
  50. <div class="row">
  51. <div class="col-xs-12 col-sm-12 col-md-12">
  52. <strong>Name : </strong>
  53. <div class="form-group">
  54. <input ng-model="form.name" type="text" placeholder="Product Name" name="name" class="form-control" required />
  55. </div>
  56. </div>
  57. <div class="col-xs-12 col-sm-12 col-md-12">
  58. <strong>Details : </strong>
  59. <div class="form-group" >
  60. <textarea ng-model="form.details" class="form-control" required>
  61. </textarea>
  62. </div>
  63. </div>
  64. </div>
  65. <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
  66. <button type="submit" ng-disabled="addProduct.$invalid" class="btn btn-primary">Submit</button>
  67. </div>
  68. </form>
  69. </div>
  70. </div>
  71. </div>
  72. </div>
  73. <!-- Edit Modal -->
  74. <div class="modal fade" id="edit-data" tabindex="-1" role="dialog" aria-labelledby="myModalForLabel">
  75. <div class="modal-dialog" role="document">
  76. <div class="modal-content">
  77. <form method="POST" name="editProduct" role="form" ng-submit="updateProduct()">
  78. <input ng-model="form.id" type="hidden" placeholder="Name" name="name" class="form-control" />
  79. <div class="modal-header">
  80. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  81. <h4 class="modal-title" id="myModalForLabel">Edit Product</h4>
  82. </div>
  83. <div id="modal_body_container" class="modal-body">
  84. <div class="row">
  85. <div class="col-xs-12 col-sm-12 col-md-12">
  86. <div class="form-group">
  87. <input ng-model="form.name" type="text" placeholder="Name" name="name" class="form-control" required />
  88. </div>
  89. </div>
  90. <div class="col-xs-12 col-sm-12 col-md-12">
  91. <div class="form-group">
  92. <textarea ng-model="form.details" class="form-control" required>
  93. </textarea>
  94. </div>
  95. </div>
  96. </div>
  97. <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
  98. <button type="submit" ng-disabled="editProduct.$invalid" class="btn btn-primary create-crud">Submit</button>
  99. </div>
  100. </form>
  101. </div>
  102. </div>
  103. </div>
  104. </div>

resources/views/htmltemplates/dirPagination.html


  1. <ul class="pagination pull-right" ng-if="1 < pages.length">
  2. <li ng-if="boundaryLinks" ng-class="{ disabled : pagination.current == 1 }">
  3. <a href="" ng-click="setCurrent(1)">&laquo;</a>
  4. </li>
  5. <li ng-if="directionLinks" ng-class="{ disabled : pagination.current == 1 }">
  6. <a href="" ng-click="setCurrent(pagination.current - 1)">&lsaquo;</a>
  7. </li>
  8. <li ng-repeat="pageNumber in pages track by $index" ng-class="{ active : pagination.current == pageNumber, disabled : pageNumber == '...' }">
  9. <a href="" ng-click="setCurrent(pageNumber)">{{ pageNumber }}</a>
  10. </li>
  11. <li ng-if="directionLinks" ng-class="{ disabled : pagination.current == pagination.last }">
  12. <a href="" ng-click="setCurrent(pagination.current + 1)">&rsaquo;</a>
  13. </li>
  14. <li ng-if="boundaryLinks" ng-class="{ disabled : pagination.current == pagination.last }">
  15. <a href="" ng-click="setCurrent(pagination.last)">&raquo;</a>
  16. </li>
  17. </ul>

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort serdivan escort sapanca escort karasu escort hendek escort ankara escort
sakarya escort kızlar sakarya escort kaliteli sakarya escort vip sakarya escort elit sakarya escort bayan sakarya escort adresleri.