في هذا المقال سوف نتحدث عن عملية الحذف بإستخدام VueJs و Laravel وللقيام بذلك
في الـ component المخصص لعرض البيانات والموجود في المسار resources/js/components/Posts/index.vue نضيف زر للحذف، مع حدث click حيث يتم تنفيذ دالة delete_post مع إرسال id الـ post
<td> <button @click="delete_post(post.id)" class="btn btn-danger">Delete</button> </td>
في داخل الدالة mounted ننشئ الدالة post_delete وهي تستقبل الـ id المراد حذفة
delete_post(id) {
swal({
title: "Are Your Shure You Want to Delete The Post ?",
icon: "warning",
buttons: ["No", "Yes"],
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
axios.delete('/api/posts/' + id).then(response => {
swal("Post Deleted Successfully");
this.getResults()
})
} else {
swal("Post Not Deleted");
}
}).catch(error => {
swal({
icon: 'error',
title: 'Error Happened'
});
});
}توضيح
نحتاج لإنشاء دالة destroy حيث تأخذ Post المراد حذفة، وبعد الحذف لا يتم إرجاع شيئ.
public function destroy(Post $post)
{
$post->delete();
return response()->noContent();
}الكود بالكامل
<template>
<div class="container">
<br/>
<select v-model="category_id" class="form-control">
<option value="">--Choose Category--</option>
<option v-for="category in categories" :value="category.id">
{{ category.name }}
</option>
</select>
<br/>
<table class="table">
<thead>
<tr>
<th scope="col">
<a href="#" @click.prevent="change_sort('id')">id</a>
<span
:class="this.sort_field === 'id' && this.sort_direction === 'asc' ? 'text-dark' : 'text-black-50'">↑</span>
<span
:class="this.sort_field === 'id' && this.sort_direction === 'desc' ? 'text-dark' : 'text-black-50'">↓</span>
</th>
<th scope="col">
<a href="#" @click.prevent="change_sort('title')">title</a>
<span
:class="this.sort_field === 'title' && this.sort_direction === 'asc' ? 'text-dark' : 'text-black-50'">↑</span>
<span
:class="this.sort_field === 'title' && this.sort_direction === 'desc' ? 'text-dark' : 'text-black-50'">↓</span>
</th>
<th scope="col">
<a href="#" @click.prevent="change_sort('body')">body</a>
<span
:class="this.sort_field === 'body' && this.sort_direction === 'asc' ? 'text-dark' : 'text-black-50'">↑</span>
<span
:class="this.sort_field === 'body' && this.sort_direction === 'desc' ? 'text-dark' : 'text-black-50'">↓</span>
</th>
<th scope="col">
<a href="#" @click.prevent="change_sort('created_at')">created_at</a>
<span
:class="this.sort_field === 'created_at' && this.sort_direction === 'asc' ? 'text-dark' : 'text-black-50'">↑</span>
<span
:class="this.sort_field === 'created_at' && this.sort_direction === 'desc' ? 'text-dark' : 'text-black-50'">↓</span>
</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr v-for="post in posts.data" :key="post.id">
<td>{{ post.id }}</td>
<td>{{ post.title }}</td>
<td>{{ post.body.substring(0, 30) }}</td>
<td>{{ post.created_at }}</td>
<td>
<router-link :to="{ name:'posts.edit', params:{ id:post.id} }" class="btn btn-success">Edit
</router-link>
</td>
<td>
<button @click="delete_post(post.id)" class="btn btn-danger">Delete</button>
</td>
</tr>
</tbody>
</table>
<pagination :data="posts" @pagination-change-page="getResults"></pagination>
</div>
</template>
<script>
export default {
data() {
return {
posts: {},
categories: {},
category_id: '',
sort_field: 'created_at',
sort_direction: 'desc',
}
},
mounted() {
axios.get('/api/category')
.then(response => {
this.categories = response.data.data;
});
this.getResults();
},
watch: {
category_id(value) {
this.getResults()
}
},
methods: {
change_sort(field) {
if (this.sort_field === field) {
this.sort_direction = this.sort_direction === 'asc' ? 'desc' : 'asc';
} else {
this.sort_field = field;
this.sort_direction = 'asc';
}
this.getResults();
},
getResults(page = 1) {
console.log(
'/api/posts?page=' + page
+ '&category_id=' + this.category_id
+ '&sort_field=' + this.sort_field
+ '&sort_direction=' + this.sort_direction
);
axios.get('/api/posts?page=' + page
+ '&category_id=' + this.category_id
+ '&sort_field=' + this.sort_field
+ '&sort_direction=' + this.sort_direction)
.then(response => {
this.posts = response.data;
});
},
delete_post(id) {
swal({
title: "Are Your Shure You Want to Delete The Post ?",
icon: "warning",
buttons: ["No", "Yes"],
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
axios.delete('/api/posts/' + id).then(response => {
swal("Post Deleted Successfully");
this.getResults()
})
} else {
swal("Post Not Deleted");
}
}).catch(error => {
swal({
icon: 'error',
title: 'Error Happened'
});
});
}
}
}
</script>