I'm beginner Vue js, I want to try make follow and unfollow system with Laravel. I'm facing problem with v-for loop for getting each data insert and toggle follow and unfollow with icon class.
this is my template file :
<template>
<ul class="suggestions-lists m-0 p-0">
<li v-for="(user, index) in users" :key="user.id" class="d-flex mb-4 align-items-center">
<h6>{{user.name}}</h6>
<div class="add-suggestion">
<a href="javascript:void(0);" @click="toggleFollow(user)">
<i :class="'text-danger '+(user ? 'ri-user-follow-line' : 'ri-user-add-line')"></i>
</a>
</div>
</li>
</ul>
</template>
This is Script :
<script>
export default {
props: ['userid'],
data: function () {
return {
users: []
}
},
methods: {
getUsers: function() {
var app = this;
axios.get('/followers')
.then(function (response) {
app.users = response.data;
})
.catch(function (error) {
console.log(error.message);
});
},
toggleFollow: function (user) {
if (this.user = !this.user) {
console.log(this.user);
this.followAdd(user)
} else {
console.log(this.user);
}
},
followAdd: function (user) {
this.user=user.id;
console.log(this.user);
axios.post('followers/store', { follower: this.userid, following: this.user }).then((response) => {
console.log(response.data)
}).catch((errors) => {
console.log(errors)
});
},
},
created() {
this.getUsers();
},
}
This is User Data getting from /followers :
[{"id":1,"name":"Rezaul Karim","email":"admin@admin.com","phone":"+8801210836617","date_of_birth":"2022-09-05","gender":"Male","created_at":"2022-09-05T09:04:45.000000Z","updated_at":"2022-09-05T09:10:46.000000Z"},{"id":3,"name":"RK","email":null,"phone":"+8801450745578","date_of_birth":"1991-01-01","gender":"Male","created_at":"2022-09-05T09:22:15.000000Z","updated_at":"2022-09-05T09:22:31.000000Z"}]
This is foolowAdd store method :
public function store(Request $request)
{
$follower = new Followers();
$follower->insert([
'follower_id' => $request->follower,
'following_id' => $request->following
]);
return response()->json($follower->get());
}
I want to toggle this icon and insert data. please help me out, How implement this.
