CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   Javascript   Laravel VueJS 2 CRUD Tutorial Part 4 (Delete Data From DataB ...

Laravel VueJS 2 CRUD Tutorial Part 4 (Delete Data From DataBase)

March 10, 2018 by SNK

Laravel VueJS Tutorial Part 4

At Last, finally we are in last stage of our laravel vuejs crud tutorial. In last post we have written about how to update records into database using vuejs and laravel.

If you are new to this article please visit our last written post at Laravel VueJS 2 Crud Application Part 3 or may be you can start from Setting Up Laravel and VueJS 2 Project. This will help you better understand on what this article is about.

Without waiting much time lets finish our final part of our laravel vuejs 2 crud tutorial series.

Lets get started

Till now our directory structure looks like this.

Final Directory Structure of an Application

To delete the records from the database we just need to follow these simple steps below.

  1. Add @click event which trigger a function and pass category id to it.
  2. Create new deleteCategory() function inside ViewCategory component methods section.
  3. Create routes in api.php and delete function inside CategoryController.php in laravel.
  4. Write a javascript code to remove the record which was pressed delete.

Lets add click event to the buttons. Open up ViewCategory.vue and changes this line.

ViewCategory
1
<a class="btn btn-danger btn-xs">Delete Category</a>

to this

ViewCategory.vue
1
<a @click="deleteCategory(category.id)" class="btn btn-danger btn-xs">Delete Category</a>

Add new function named deleteCategory() just after viewCategories() function.

ViewCategory.vue
JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
deleteCategory(id) {
                let instance = this;
                axios.get('api/categories/'+id+'/delete')
                    .then(function (response) {
                       $('#div'+id).fadeOut(300, function() {
                            console.log("deleted");
                        })
 
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            }

Also,

Change this line :

1
<tr v-for="(category,index) in categories_list">

to this :

1
<tr v-for="(category,index) in categories_list" :id="'div'+category.id">

Now we have finished our task in VueJS. Time to configure routes and controller in laravel.

Go to routes/api.php and add this line.

api.php
PHP
1
Route::get("categories/{id}/delete",'CategoryController@deleteCategory');

Go to CategoryController.php inside app/Http/Controllers and add this function.

CategoryController.php
PHP
1
2
3
4
5
public function deleteCategory($id) {
        $data = tbl_category::find($id);
        $data->delete();
        return Response::json(true);
    }

Finally hit php artisan serve and npm run watch in order to test the application.

Good going,  you have finally completed laravel vuejs crud tutorial series. If you want to take a look at the sample application we build during our series, you can download them by clicking this link.

If you have any problem, you can mention via comments or contact us via contact page.

SHARE ON
Buffer

Enjoyed this article?

Like us on

Javascript Laravel

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

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

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.