Laravel 5 and Vue JS CRUD with Pagination example and demo from scratch

Laravel 5 and Vue JS CRUD with Pagination example and demo from scratch

In this post we will give you information about Laravel 5 and Vue JS CRUD with Pagination example and demo from scratch. Hear we will give you detail about Laravel 5 and Vue JS CRUD with Pagination example and demo from scratchAnd how to use it also give you demo for it if it is necessary.

In Todays, Most popular JS Framework are Angular JS and Vue JS. Angular JS and Vue JS are a very user friendly JS Framework and most popular. It provides to manage whole project or application without refresh page and powerful jquery validation.

In this post i going to lean how to create CRUD(Create, Read, Update and Delete) application with pagination using Laravel 5. In my previous tutorial i added crud application using Angular JS, if you want to see then click here: Laravel 5.2 and AngularJS CRUD with Search and Pagination Example.Laravel 5.2 and AngularJS CRUD with Search and Pagination Example.

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 IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;


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 IlluminateDatabaseEloquentModel;


class Item extends Model

{


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


}

Also see:Laravel 5.2 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 AppHttpControllers;


use IlluminateHttpRequest;

use AppHttpControllersController;

use AppItem;


class VueItemController extends Controller

{


public function manageVue()

{

return view('manage-vue');

}


/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

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 IlluminateHttpRequest $request

* @return IlluminateHttpResponse

*/

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 IlluminateHttpRequest $request

* @param int $id

* @return IlluminateHttpResponse

*/

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 IlluminateHttpResponse

*/

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 id="manage-vue">


<div >

<div >

<div >

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

</div>

<div >

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

Create Item

</button>

</div>

</div>

</div>


<!-- Item Listing -->

<table >

<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 @click.prevent="editItem(item)">Edit</button>

<button @click.prevent="deleteItem(item)">Delete</button>

</td>

</tr>

</table>


<!-- Pagination -->

<nav>

<ul >

<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:>

<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 id="create-item" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">

<div role="document">

<div >

<div >

<button type="button" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>

<h4 id="myModalLabel">Create Item</h4>

</div>

<div >


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


<div >

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

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

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

</div>


<div >

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

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

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

</div>


<div >

<button type="submit" >Submit</button>

</div>


</form>


</div>

</div>

</div>

</div>


<!-- Edit Item Modal -->

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

<div role="document">

<div >

<div >

<button type="button" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>

<h4 id="myModalLabel">Edit Item</h4>

</div>

<div >


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


<div >

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

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

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

</div>


<div >

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

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

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

</div>


<div >

<button type="submit" >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

Also see: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

from = 1;

}

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

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

to = this.pagination.last_page;

}

var pagesArray = [];

while (from

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);

}


}


});

Now you can run and check your own….

Hope this code and post will helped you for implement Laravel 5 and Vue JS CRUD with Pagination example and demo from scratch. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  77  =  81

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US