Hello artisans, welcome to justlaravel.com. Here am going to show you how to perform CRUD operations with Vue.js and Laravel. I have previously discussed Vue js in my previous tutorials where I created a simple search app, and some introduction to Vue, so here I am going to make a complete CRUD app using Vue.js and Laravel.
Let’s get started!
Setting up Vue js
We first need to setup Vue in our project. Its a breeze in Laravel as it is already included with Vue, we just need to install some modules to get started with Vue.
- Create a laravel project:
laravel new VueCRUD
- Install PHP packages: go to VueCRUD directory and run
- Generate a key:
php artisan key:generate
- Install node packages:
- Run the app:
npm run watchto make the changes to take effect immediately and also run
php artisan servefor the checking the actual app.
Overview of the app
The app will display a list of people with the name, age and their profession, with edit and delete options, and 3 fields to add a new item.
Initialize Vue js
All the Vue(js) part is written in file
First I will create a new vue instance, and all the vue part is handled in an element with id
vue-crud-wrapper i.e all the HTML content is wrapped in a div with id
Create a new vue instance(app.js)
Add Person Form(Create Operation)
Here a form is shown with 3 fields name, age, and profession with a button which when clicked calls the action in the js and performs the add operation.
In the above snippet, for button, I used
@click.prevent="createItem()" which calls
createItem vue method which is declared in the file
So in the vue, I initialize some variables, which can be used in the methods like
Here I also check for empty fields and throw an error when the input field is empty, so I initialize that
So when the fields are empty, I change the hasError variable to false, and a condition is written in the view to display error when the variable is set to false.
Here the class
hidden is bonded with the variable
hasError, so if
true that class is applied.
That is why I set
false when there is an error.
Next, if all the fields are filled, I make a post call using axios and pass the data entered in the form. All the data that to be stored in the input variable and it is passed as an argument to the post function.
So this route
/vueitems will call to a controller where the data is stored in the database.
web.php file at
We can create a new controller by running the command
php artisan:make controller MainController
Now in the MainController(/app/Http/Controllers),
Here I used a model
Data which has all the details related to database table the data is being stored.
So in the
app directory, I create a new model named
Data.php with the following class.
$data->save(); is executed, all the data will be stored in the
vueCrudData table in the database.
Here the data is displayed in the table as shown in the image above. I use some vue properties and binding to the table elements so the data can be displayed as I wanted.
In the above snippet, I used
v-for directive to repeat all the
items, also used some click methods which are used to update and delete items, which I will discuss in next section.
So the data in items variable is stored through js, let’s see it.
There is a hook in vue called
mounted. this hook calls immediately when a page loads or vue instance loads. So here in this hook, I call a method called getVueItems(), in this method a call to Laravel Controller and there it fetches the data and returns the same to view.
Now the data is set in the variable
items, so iterating(v-for) this variable shows us all the data we need for the view.
So the route
/vueitems routes to,
readItems function in MainController,
The above snippet is very simple and straightforward, it gets all the data from the database and returns it and that data is shown in the view using Vue js.
The table containing all the data is presented with two buttons “Edit” and “Delete” under “Actions” column.
So when edit button is clicked, a modal is shown with that particular row’s data and an option to edit them.
@click="showModal= true; triggers the modal,
v-if directive is responsible for toggling the modal open and close. So if the
showModal variable is true modal is displayed else it is closed.
true, a method
setVal with id, name, age, and profession is set. So this function is used to display the data in the modal, the function’s arguments are taken and assigned to other variables, which can be accessed in the view.
Here the function’s arguments val_id, val_name, val_age, val_profession are assigned to variables e_id, e_name, e_age, e_profession respectively.
So now these variables are used in the modal’s body element.
If you see the input element’s value
:value="this.e_age" is the value that is set in the js script above in
In the modal footer section, two buttons, “Cancel” and “Update” are present. So when cancel is clicked, the
showModal variable is set to
false so the modal closes, when Update is clicked a method
editItem() is called.
editItem() vue js method
The first four lines gets all the info from the modal using
getElementById and uses those to pass as a an arguments to axios post method.
So in response method,
getVueItems() is again called so the items are updated in the view.
And the modal is closed by setting
edititems/id route is set to,
The editItem function in MainController,
The above snippet will fetch the row with the given id, and update its elements name, age, and profession.
In the actions column there is an icon for delete,
So when clicked it calls
this method simply calls
/vueitems/id route which calls Laravel deleteItem function.
deleteItem function in MainController,
The item gets deleted, and in the vue response function
getVueItems is called, do the items are updated.
So finally all the CRUD(Create, Read, Update, Delete) operations are performed using vue js and laravel.
You can also find all Vue related tutorials at https://justlaravel.com/category/vue-js/