In this tutorial, we’ll build a complete CRUD application using VueJS and Laravel.
For illustration, consider the following simple app:
Contents
- Before we kick off
- Introduction
- Setting up Laravel and VueJs
- Initialize VueJS
- Create Operation
- Read Operation
- Update Operation
- Delete Operation
- Conclusion
Before we kick off
Learn Vue.js and modern, cutting-edge front-end technologies from core-team members and industry experts with our premium tutorials and video courses on VueSchool.io.
prerequisites
Introduction
CRUD is an acronym for the four basic operations: Create, Read, Update, Delete. Most applications have some kind of CRUD functionality, and we can assume that every programmer had to deal with CRUD at some point. A CRUD application is one that uses forms to get data into and out of a database.
Setting up Laravel and VueJs
We first need to setup Laravel. For that, create a project named vue_laravel.
Laravel UI provides a way to install bootstrap, vue, and react setup. If you want to install VueJS in your laravel project, install the following laravel UI composer package.
Now we’re ready to install VueJs.
Suppose you would like to install Vue with the auth run the following command. But it is no need for our example.
So now, please run the command below to compile your fresh scaffolding.
Open your /resources/js/app.js
, and you’ll see the Vue starter code. All the VueJs part we’ll write in this file app.js
To ensure that Webpack will automatically recompile your assets when it detects a change run this command:
In case you want to go deeper in Laravel Mix, click here to get it done, using the official documentation.
We need to create the controller, model, the table to save the store the data, and the routes. For that, run the following commands:
Controller:
Model:
In cars.php
file paste this:
Make sure you have your Laravel project connected to your database.
Migration:
Migrate it!
Update your routes. Add this code in your route file /routes/web.php
:
We are ready to go. Now you can run the app and start coding.
Initialize VueJS
In the app.js you can see that we have an idd named #app. It means that all the Vue part is handled in an element with an id app.
That’s why all the HTML content is wrapped in an element with id app
. Open the welcome.blade.php file and initialize Vue.
Create Operation
Let’s create a quick bootstrap form to handle this operation. Inside of div element paste this code:
In above snippet we have:
- An alert block that will display a message whenever fields are empty. The display block is controlled by
hasError
variable that is declared in app.js. - Form Input Bindings
- For button, we use
@click.prevent="createCar()"
which callscreateCar
Vue method that is declared in app.js.
So in the vue, we need to initialize the variables that we’ll need for this operation.
We use the function below to insert data into the database, and we also check if the fields are empty.
In CarsController /app/Http/Controllers/CarsController.php
add this function:
Read Operation
To get all the data we need to create a function named getCars() in our app.js file.
Let’s update our controller to fetch the data (CarsController.php):
We would like to call the getCars()
function when the page loads. To do so, there is a hook in vue called mounted
. In this hook, we call the method getCars()
:
app.js
To display these data we need to add a table in our view /resources/view/welcome.blade.php
.
Now the data is set in the variable cars
, so iterating with v-for
this variable shows us all the data we need for the view.
Update Operation
In our table we have two buttons in each row: “delete” and “edit”.
When edit button is clicked, a modal is shown with that particular row’s data and an option to edit them. We can use the Bootstrap modal.
We need these fields to display the data we want to edit:
After showing the model, the setVal method is set. We use this function to display the data in our model.
To work with this function, first we need to add this variables in our vue file (app.js):
And setup the setVal
function:
Now we can use these variables in our model.
In our modal footer, you can see that we have two buttons. One to edit and the other one to close the model.
Open app.js and create the function to edit the data.
And do not forget to update our CarsController.php. This snippet will fetch the row with the given id, and update its elements make, and model.
Delete Operation
On each row we see the icon to delete an item. So when clicked it calls deleteCar() function.
app.js
We need to update the CarsController.php:
Conclusion
You probably notice that in every function, we use _this.getCars(). This is to update the UI after data changes.
Now you can use the CRUD operations for your needs. you can Check out CodeSource here.
Thanks for reading.