John Davidson

php - Laravel mysql data input.. Check if Users follow each other

0 comments
Message:




public function follow(Request $request){

$response = array();
$response['code'] = 400;

$following_user_id = $request->input('following');
$follower_user_id = $request->input('follower');
$element = $request->input('element');
$size = $request->input('size');

$following = User::find($following_user_id);
$follower = User::find($follower_user_id);

if ($following && $follower && ($following_user_id == Auth::id() || $follower_user_id == Auth::id())){

$relation = UserFollowing::where('following_user_id', $following_user_id)->where('follower_user_id', $follower_user_id)->get()->first();

if ($relation){
if ($relation->delete()){
$response['code'] = 200;
if ($following->isPrivate()) {
$response['refresh'] = 1;
}
}
}else{
$relation = new UserFollowing();
$relation->following_user_id = $following_user_id;
$relation->follower_user_id = $follower_user_id;
if ($following->isPrivate()){
$relation->allow = 0;
}else{
$relation->allow = 1;
}
if ($relation->save()){
$response['code'] = 200;
$response['refresh'] = 0;

if ($following && $follower){

$relationz = new UserRelationship();
$relationz->main_user_id = $following_user_id;
$relationz->relation_type = 1;
$relationz->related_user_id = $follower_user_id;
$relationz->allow = 1;
$relationz->save();

}
}
}

if ($response['code'] == 200){
$response['button'] = sHelper::followButton($following_user_id, $follower_user_id, $element, $size);
}
}

return Response::json($response);

}


My Answer:
This code snippet is a Laravel function that handles the follow/unfollow functionality between users. Here's a breakdown of what the code does:

1. It receives the following parameters from the request: following_user_id, follower_user_id, element, and size.
2. It retrieves the User models for the following and follower users using their IDs.
3. It checks if the authenticated user is either the following or follower user.
4. It checks if there is already a UserFollowing relationship between the following and follower users. If there is, it deletes the relationship. If not, it creates a new UserFollowing relationship.
5. If the UserFollowing relationship is successfully created or deleted, it updates the response code to 200 and sets the refresh flag based on the privacy settings of the following user.
6. If the relationship is created, it also creates a UserRelationship entry to establish a relationship between the following and follower users.
7. It generates a follow/unfollow button based on the current relationship status between the users.
8. It returns a JSON response with the updated response code and button HTML.

Overall, this code snippet handles the follow/unfollow logic between users and updates the database accordingly.

Rate this post

4 of 5 based on 6209 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us