thinkphp5 realizes circle of friends (2)

I will not repeat the ideas mentioned above, and then start the friend list directly.
This table is used to record friend relationships, and the implementation is also very simple, that is, the front end stores the data in the background.

Code

public function addFriend()
    {
        //get json data
        $res = input('post.');
        $username = $res["username"];
        $friend = $res["friend"];

        // Determine if the data is received
        if (!$username || !$friend)
        {
            echo "Data could not be received";
            exit;
        }
        else
        {
            //add data to database
            $data = ['user' => $username, 'friend' => $friend];
            $res = Db::table("friend")
                ->insert($data);

            //Here the user name and friend are transferred to save one more time for the convenience of operation. In fact, it is enough to save one, but it is not so simple to realize the search when using their relationship later.
            $data2 = ['user' => $friend, 'friend' => $username];
            $res2 = Db::table("friend")
                ->insert($data2);
            if (!!$res||!!$res2)
            {
                echo "Insert data successfully";
                exit;
            }
            else
            {
                echo "Failed to insert data";
                exit;
            }

        }
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324864519&siteId=291194637