CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other
⌕
Popular learning topics
PHP Laravel JavaScript CSS SQL Python Vue.js WordPress

16 January, 2023

Laravel 9 and Vue JS CRUD with Pagination begning to scratch

 Programing Coderfunda     January 16, 2023     Laravel, Vuejs     No comments   

 In Todays, Most well known JS Structure are Precise JS and Vue JS. Rakish JS and Vue JS are an exceptionally easy to understand JS Structure and generally well known. It gives to oversee entire task or application without invigorate page and strong jquery approval.


In this post I going to incline how to make CRUD(Create, Read, Update and Erase) application with pagination utilizing Laravel 5. In my past instructional exercise I added muck application utilizing Precise JS, if you need to see then click here:


In this example i added "Item Management" with you can do several option like as bellow:

1. Item Listing

2. Item Create

3. Item Edit

4. Item Delete

you can implement crud application from scratch, so no worry if you can implement through bellow simple step. After create successful example, you will find layout as bellow:

Preview:


Step 1: Laravel Installation

In first step, If you haven't installed Laravel in your system then you have to run bellow command and get fresh Laravel project.

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create items table and model

In this step we have to create migration for items table using Laravel 5 php artisan command, so first fire bellow command:

php artisan make:migration create_items_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create items table.

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;


class CreateItemsTable extends Migration

{


public function up()

{

Schema::create('items', function (Blueprint $table) {

$table->increments('id');

$table->string('title');

$table->text('description');

$table->timestamps();

});

}


public function down()

{

Schema::drop("items");

}

}

After create "items" table you should create Item model for items, so first create file in this path app/Item.php and put bellow content in item.php file:

app/Item.php

namespace App;


use Illuminate\Database\Eloquent\Model;


class Item extends Model

{


public $fillable = ['title','description'];


}

Read Also: Laravel 9 and AngularJS CRUD with Search and Pagination Example.

Step 3: Add Route and Controller

Now we have to add route for items CRUD and pagination, in this example i added resource route and one for manage-vue for application, if we add resource route then it will add index, create, edit and delete route automatically. So add bellow line in your route file.

app/Http/routes.php

Route::get('manage-vue', 'VueItemController@manageVue');

Route::resource('vueitems','VueItemController');

Ok, now we should create new controller as VueItemController in this path app/Http/Controllers/VueItemController.php. this controller will manage all route method:

app/Http/Controllers/VueItemController.php

namespace App\Http\Controllers;


use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Item;


class VueItemController extends Controller

{


public function manageVue()

{

return view('manage-vue');

}


/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$items = Item::latest()->paginate(5);


$response = [

'pagination' => [

'total' => $items->total(),

'per_page' => $items->perPage(),

'current_page' => $items->currentPage(),

'last_page' => $items->lastPage(),

'from' => $items->firstItem(),

'to' => $items->lastItem()

],

'data' => $items

];


return response()->json($response);

}


/**

* Store a newly created resource in storage.

*

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

*/

public function store(Request $request)

{

$this->validate($request, [

'title' => 'required',

'description' => 'required',

]);


$create = Item::create($request->all());


return response()->json($create);

}


/**

* Update the specified resource in storage.

*

* @param \Illuminate\Http\Request $request

* @param int $id

* @return \Illuminate\Http\Response

*/

public function update(Request $request, $id)

{

$this->validate($request, [

'title' => 'required',

'description' => 'required',

]);


$edit = Item::find($id)->update($request->all());


return response()->json($edit);

}


/**

* Remove the specified resource from storage.

*

* @param int $id

* @return \Illuminate\Http\Response

*/

public function destroy($id)

{

Item::find($id)->delete();

return response()->json(['done']);

}

}

Step 4: Create Blade File

In this step we have to create only one blade file that will manage create, update and delete operation of items module with vue js.

In this file i added jquery, bootstrap js and css, vue.js, vue-resource.min.js, toastr js and css for notification in this blade file.

So, let's create manage-vue.blade.php file on following way:

resources/views/manage-vue.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Vue JS Item CRUD</title>

<meta id="token" name="token" value="{{ csrf_token() }}">

<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css">

</head>

<body>


<div class="container" id="manage-vue">


<div class="row">

<div class="col-lg-12 margin-tb">

<div class="pull-left">

<h2>Laravel Vue JS Item CRUD</h2>

</div>

<div class="pull-right">

<button type="button" class="btn btn-success" data-toggle="modal" data-target="#create-item">

Create Item

</button>

</div>

</div>

</div>


<!-- Item Listing -->

<table class="table table-bordered">

<tr>

<th>Title</th>

<th>Description</th>

<th width="200px">Action</th>

</tr>

<tr v-for="item in items">

<td>@{{ item.title }}</td>

<td>@{{ item.description }}</td>

<td>

<button class="btn btn-primary" @click.prevent="editItem(item)">Edit</button>

<button class="btn btn-danger" @click.prevent="deleteItem(item)">Delete</button>

</td>

</tr>

</table>


<!-- Pagination -->

<nav>

<ul class="pagination">

<li v-if="pagination.current_page > 1">

<a href="#" aria-label="Previous"

@click.prevent="changePage(pagination.current_page - 1)">

<span aria-hidden="true">«</span>

</a>

</li>

<li v-for="page in pagesNumber"

v-bind:class="[ page == isActived ? 'active' : '']">

<a href="#"

@click.prevent="changePage(page)">@{{ page }}</a>

</li>

<li v-if="pagination.current_page < pagination.last_page">

<a href="#" aria-label="Next"

@click.prevent="changePage(pagination.current_page + 1)">

<span aria-hidden="true">»</span>

</a>

</li>

</ul>

</nav>


<!-- Create Item Modal -->

<div class="modal fade" id="create-item" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">

<div class="modal-dialog" role="document">

<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">Create Item</h4>

</div>

<div class="modal-body">


<form method="POST" enctype="multipart/form-data" v-on:submit.prevent="createItem">


<div class="form-group">

<label for="title">Title:</label>

<input type="text" name="title" class="form-control" v-model="newItem.title" />

<span v-if="formErrors['title']" class="error text-danger">@{{ formErrors['title'] }}</span>

</div>


<div class="form-group">

<label for="title">Description:</label>

<textarea name="description" class="form-control" v-model="newItem.description"></textarea>

<span v-if="formErrors['description']" class="error text-danger">@{{ formErrors['description'] }}</span>

</div>


<div class="form-group">

<button type="submit" class="btn btn-success">Submit</button>

</div>


</form>


</div>

</div>

</div>

</div>


<!-- Edit Item Modal -->

<div class="modal fade" id="edit-item" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">

<div class="modal-dialog" role="document">

<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">Edit Item</h4>

</div>

<div class="modal-body">


<form method="POST" enctype="multipart/form-data" v-on:submit.prevent="updateItem(fillItem.id)">


<div class="form-group">

<label for="title">Title:</label>

<input type="text" name="title" class="form-control" v-model="fillItem.title" />

<span v-if="formErrorsUpdate['title']" class="error text-danger">@{{ formErrorsUpdate['title'] }}</span>

</div>


<div class="form-group">

<label for="title">Description:</label>

<textarea name="description" class="form-control" v-model="fillItem.description"></textarea>

<span v-if="formErrorsUpdate['description']" class="error text-danger">@{{ formErrorsUpdate['description'] }}</span>

</div>


<div class="form-group">

<button type="submit" class="btn btn-success">Submit</button>

</div>


</form>


</div>

</div>

</div>

</div>


</div>


<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script>


<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>

<link href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css" rel="stylesheet">


<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script>

<script type="text/javascript" src="https://cdn.jsdelivr.net/vue.resource/0.9.3/vue-resource.min.js"></script>


<script type="text/javascript" src="/js/item.js"></script>


</body>

</html>

Step 5: Create JS File

In last step we require to create one item.js file, first we have to create new folder "js" in public directory and then create item.js file inside of this folder.

item.js file through we can manage item pagination, create edit delete task and also ajax method.

public/js/item.js

Read Also: Laravel 5.2 - User ACL Roles and Permissions with Middleware using entrust from Scratch Tutorial

Vue.http.headers.common['X-CSRF-TOKEN'] = $("#token").attr("value");


new Vue({


el: '#manage-vue',


data: {

items: [],

pagination: {

total: 0,

per_page: 2,

from: 1,

to: 0,

current_page: 1

},

offset: 4,

formErrors:{},

formErrorsUpdate:{},

newItem : {'title':'','description':''},

fillItem : {'title':'','description':'','id':''}

},


computed: {

isActived: function () {

return this.pagination.current_page;

},

pagesNumber: function () {

if (!this.pagination.to) {

return [];

}

var from = this.pagination.current_page - this.offset;

if (from < 1) {

from = 1;

}

var to = from + (this.offset * 2);

if (to >= this.pagination.last_page) {

to = this.pagination.last_page;

}

var pagesArray = [];

while (from <= to) {

pagesArray.push(from);

from++;

}

return pagesArray;

}

},


ready : function(){

this.getVueItems(this.pagination.current_page);

},


methods : {


getVueItems: function(page){

this.$http.get('/vueitems?page='+page).then((response) => {

this.$set('items', response.data.data.data);

this.$set('pagination', response.data.pagination);

});

},


createItem: function(){

var input = this.newItem;

this.$http.post('/vueitems',input).then((response) => {

this.changePage(this.pagination.current_page);

this.newItem = {'title':'','description':''};

$("#create-item").modal('hide');

toastr.success('Item Created Successfully.', 'Success Alert', {timeOut: 5000});

}, (response) => {

this.formErrors = response.data;

});

},


deleteItem: function(item){

this.$http.delete('/vueitems/'+item.id).then((response) => {

this.changePage(this.pagination.current_page);

toastr.success('Item Deleted Successfully.', 'Success Alert', {timeOut: 5000});

});

},


editItem: function(item){

this.fillItem.title = item.title;

this.fillItem.id = item.id;

this.fillItem.description = item.description;

$("#edit-item").modal('show');

},


updateItem: function(id){

var input = this.fillItem;

this.$http.put('/vueitems/'+id,input).then((response) => {

this.changePage(this.pagination.current_page);

this.fillItem = {'title':'','description':'','id':''};

$("#edit-item").modal('hide');

toastr.success('Item Updated Successfully.', 'Success Alert', {timeOut: 5000});

}, (response) => {

this.formErrorsUpdate = response.data;

});

},


changePage: function (page) {

this.pagination.current_page = page;

this.getVueItems(page);

}


}


});

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • What is the difference between migrate, rollback, refresh, and fresh?
      migrate applies outstanding migrations. rollback reverses the latest migration batch. refresh rolls back migrations and runs them again. f...
  • Detecting .NET8 with Inno Setup and InnoDependenciyInstaller
    I am looking at using InnoDependencyInstaller and it has a function that it uses under the hood: Dependency_AddDotNet80 procedure Depend...
  • How to Create a WordPress Theme: Build a Simple Demo Theme
      Creating a basic WordPress theme is a great way to learn PHP, HTML, CSS, and WordPress development. Below, we'll build a small working...
  • What is the difference between authentication and authorization in Laravel?
      Authentication answers: "Who are you?" Authorization answers: "Are you allowed to perform this action?" Authentication...
  • Python Tutorial
      Python   is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1069)
  • Laravel Tutorials (23)
  • Laravel-Question (36)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (3)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (69)
  • Wordpress (158)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • ►  2026 (333)
    • ►  09/27 - 10/04 (66)
    • ►  09/20 - 09/27 (52)
    • ►  09/13 - 09/20 (10)
    • ►  08/30 - 09/06 (87)
    • ►  08/16 - 08/23 (1)
    • ►  08/02 - 08/09 (8)
    • ►  07/26 - 08/02 (108)
    • ►  06/28 - 07/05 (1)
  • ►  2025 (4)
    • ►  07/06 - 07/13 (2)
    • ►  06/29 - 07/06 (2)
  • ►  2024 (486)
    • ►  09/15 - 09/22 (30)
    • ►  09/08 - 09/15 (35)
    • ►  09/01 - 09/08 (35)
    • ►  08/11 - 08/18 (2)
    • ►  08/04 - 08/11 (33)
    • ►  07/28 - 08/04 (30)
    • ►  07/07 - 07/14 (11)
    • ►  06/30 - 07/07 (35)
    • ►  06/23 - 06/30 (5)
    • ►  06/02 - 06/09 (31)
    • ►  05/26 - 06/02 (20)
    • ►  05/05 - 05/12 (29)
    • ►  04/28 - 05/05 (26)
    • ►  04/07 - 04/14 (10)
    • ►  03/31 - 04/07 (34)
    • ►  03/24 - 03/31 (10)
    • ►  03/03 - 03/10 (35)
    • ►  02/25 - 03/03 (15)
    • ►  02/04 - 02/11 (22)
    • ►  01/28 - 02/04 (30)
    • ►  01/07 - 01/14 (8)
  • ▼  2023 (484)
    • ►  12/31 - 01/07 (35)
    • ►  12/24 - 12/31 (10)
    • ►  12/03 - 12/10 (33)
    • ►  11/26 - 12/03 (20)
    • ►  11/05 - 11/12 (35)
    • ►  10/29 - 11/05 (20)
    • ►  10/22 - 10/29 (9)
    • ►  10/15 - 10/22 (7)
    • ►  10/08 - 10/15 (9)
    • ►  10/01 - 10/08 (10)
    • ►  09/24 - 10/01 (9)
    • ►  09/17 - 09/24 (9)
    • ►  09/10 - 09/17 (7)
    • ►  09/03 - 09/10 (9)
    • ►  08/27 - 09/03 (9)
    • ►  08/20 - 08/27 (8)
    • ►  08/13 - 08/20 (8)
    • ►  08/06 - 08/13 (8)
    • ►  07/30 - 08/06 (8)
    • ►  07/23 - 07/30 (7)
    • ►  07/16 - 07/23 (8)
    • ►  07/09 - 07/16 (7)
    • ►  07/02 - 07/09 (8)
    • ►  06/25 - 07/02 (7)
    • ►  06/18 - 06/25 (7)
    • ►  06/11 - 06/18 (7)
    • ►  06/04 - 06/11 (11)
    • ►  05/28 - 06/04 (7)
    • ►  05/21 - 05/28 (8)
    • ►  05/14 - 05/21 (11)
    • ►  05/07 - 05/14 (7)
    • ►  04/30 - 05/07 (7)
    • ►  04/23 - 04/30 (8)
    • ►  04/16 - 04/23 (9)
    • ►  04/09 - 04/16 (7)
    • ►  04/02 - 04/09 (4)
    • ►  03/26 - 04/02 (21)
    • ►  03/19 - 03/26 (2)
    • ►  03/12 - 03/19 (9)
    • ►  03/05 - 03/12 (26)
    • ►  02/26 - 03/05 (25)
    • ▼  01/15 - 01/22 (7)
      • Laravel 9 and Vue JS CRUD with Pagination begning ...
      • Vue JS Scroll to component in div utilizing vue-sc...
      • Laravel 9 Dynamic Form Validation in VueJs with PHP
      • Laravel 9 Dynamic Ajax Autocomplete using Vue.js
      • Laravel Dynamic Dependent Dropdown using VueJS and...
      • Laravel Vue JS Pagination
      • Example of an infinite scroll in Laravel and Vue JS
    • ►  01/08 - 01/15 (1)
  • ►  2022 (1037)
    • ►  12/11 - 12/18 (13)
    • ►  12/04 - 12/11 (1)
    • ►  11/27 - 12/04 (40)
    • ►  11/06 - 11/13 (1)
    • ►  10/16 - 10/23 (13)
    • ►  09/04 - 09/11 (5)
    • ►  08/21 - 08/28 (24)
    • ►  08/14 - 08/21 (24)
    • ►  07/03 - 07/10 (9)
    • ►  06/19 - 06/26 (3)
    • ►  05/29 - 06/05 (3)
    • ►  05/22 - 05/29 (3)
    • ►  05/15 - 05/22 (109)
    • ►  05/01 - 05/08 (7)
    • ►  04/24 - 05/01 (7)
    • ►  04/17 - 04/24 (64)
    • ►  04/10 - 04/17 (115)
    • ►  04/03 - 04/10 (73)
    • ►  03/27 - 04/03 (77)
    • ►  03/13 - 03/20 (2)
    • ►  03/06 - 03/13 (25)
    • ►  02/27 - 03/06 (18)
    • ►  02/20 - 02/27 (153)
    • ►  02/13 - 02/20 (187)
    • ►  01/30 - 02/06 (45)
    • ►  01/23 - 01/30 (15)
    • ►  01/16 - 01/23 (1)
  • ►  2021 (412)
    • ►  10/24 - 10/31 (2)
    • ►  07/25 - 08/01 (1)
    • ►  07/11 - 07/18 (10)
    • ►  06/13 - 06/20 (29)
    • ►  05/23 - 05/30 (1)
    • ►  05/02 - 05/09 (24)
    • ►  04/25 - 05/02 (24)
    • ►  04/18 - 04/25 (112)
    • ►  04/11 - 04/18 (1)
    • ►  04/04 - 04/11 (6)
    • ►  03/28 - 04/04 (86)
    • ►  03/21 - 03/28 (19)
    • ►  03/14 - 03/21 (2)
    • ►  03/07 - 03/14 (10)
    • ►  02/28 - 03/07 (1)
    • ►  02/21 - 02/28 (29)
    • ►  02/14 - 02/21 (13)
    • ►  02/07 - 02/14 (12)
    • ►  01/31 - 02/07 (6)
    • ►  01/17 - 01/24 (2)
    • ►  01/10 - 01/17 (8)
    • ►  01/03 - 01/10 (14)
  • ►  2020 (376)
    • ►  12/27 - 01/03 (37)
    • ►  12/20 - 12/27 (92)
    • ►  12/13 - 12/20 (29)
    • ►  12/06 - 12/13 (37)
    • ►  11/29 - 12/06 (4)
    • ►  11/15 - 11/22 (14)
    • ►  11/08 - 11/15 (8)
    • ►  11/01 - 11/08 (2)
    • ►  10/18 - 10/25 (14)
    • ►  10/11 - 10/18 (16)
    • ►  10/04 - 10/11 (10)
    • ►  09/20 - 09/27 (10)
    • ►  09/06 - 09/13 (19)
    • ►  08/30 - 09/06 (26)
    • ►  08/23 - 08/30 (4)
    • ►  08/16 - 08/23 (2)
    • ►  07/12 - 07/19 (48)
    • ►  05/17 - 05/24 (2)
    • ►  01/05 - 01/12 (2)
  • ►  2019 (74)
    • ►  07/07 - 07/14 (6)
    • ►  06/16 - 06/23 (6)
    • ►  02/10 - 02/17 (17)
    • ►  01/13 - 01/20 (37)
    • ►  01/06 - 01/13 (8)
  • ►  2018 (376)
    • ►  12/30 - 01/06 (24)
    • ►  12/16 - 12/23 (8)
    • ►  12/09 - 12/16 (98)
    • ►  12/02 - 12/09 (16)
    • ►  11/18 - 11/25 (36)
    • ►  11/04 - 11/11 (18)
    • ►  10/28 - 11/04 (10)
    • ►  10/21 - 10/28 (26)
    • ►  10/14 - 10/21 (52)
    • ►  10/07 - 10/14 (4)
    • ►  09/30 - 10/07 (2)
    • ►  09/23 - 09/30 (68)
    • ►  09/16 - 09/23 (4)
    • ►  09/09 - 09/16 (4)
    • ►  08/26 - 09/02 (6)

Data Publish News

Loading...

Al Jazeera – Breaking News, World News and Video from Al Jazeera

Loading...

Laravel News

Loading...

Copyright © CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda