简体   繁体   English

如何使用邮递员构建 api 以在 Laravel 中关注和取消关注用户

[英]How to Build an api to follow and unfollow user in laravel with postman

How to create an Api in laravel to follow and unfollow user如何在 Laravel 中创建 Api 以关注和取消关注用户

public function follow(){
    $id = Auth::id();
    $result = User::where('id', '!=', $id)->get();
    //return $result;
    return response()->json(['data' => $result], 200,[],JSON_NUMERIC_CHECK);
}

public function followUser(user $user){
    if (!Auth::user()->isFollowing($user_id)){
        Auth::user()->follows()->create([
          'target_id' =>$user_id,
        ]);

        return response()->json(['sucess'=>'sucessfully followed']);
     }else{
        return response()->json(['oops'=>'u already followed ']);
     }
}

public function unfollowUser(User $user)
{
    if (Auth::user()->isFollowing($user->id)) {
        $follow = Auth::user()->follows()->where('target_id', $user->id)->first();
        $follow->delete();

        return response()->json(['success', 'You are no longer friends with '. $user->name]);
    } else {
        return response()->json(['error', 'You are not following this person']);
    }
}

here is my code and this is my route to use in postman这是我的代码,这是我在邮递员中使用的路线

Route::post('/followUser/{user}','Api\FollowuserController@followUser');
Route::get('/unfollowUser/{user}','Api\FollowuserController@unfollowUser');

i am using the route to follow with sessionid on user id but getting nothing ... how should i do this我正在使用路由来跟踪用户 ID 上的 sessionid,但什么也没得到……我应该怎么做

I suggest you for route我建议你路线

Route::post('users/{id}/action', 'API\UserController@action');

and for body request:和身体要求:

{
  "act":"follow"   //or unfollow
}

table:桌子:

public function up()
{
    Schema::create('action_logs', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->integer('object_id')->unsigned();
        $table->enum('object_type',['USER','PAGE','POST']);
        $table->enum('act',['FOLLOW','UNFOLLOW','VISIT' ,'LIKE','DISLIKE']);
        $table->timestamp('created_at');

        $table->foreign('user_id')->references('id')->on('users')
            ->onDelete('restrict')
            ->onUpdate('restrict');
    });

   //Or to make it easier 
    Schema::create('follower_following', function (Blueprint $table) {
        $table->integer('follower_id')->unsigned();
        $table->integer('following_id')->unsigned();
        $table->primary(array('follower_id', 'following_id'));

        $table->foreign('follower_id')->references('id')->on('users')
            ->onDelete('restrict')
            ->onUpdate('restrict');

        $table->foreign('following_id')->references('id')->on('users')
            ->onDelete('restrict')
            ->onUpdate('restrict');
    });

in model (user.php)在模型中(user.php)

public function followers()
{
    return $this->belongsToMany('App\User', 'follower_following', 'following_id', 'follower_id')
        ->select('id', 'username', 'name','uid');
}


public function following()
{
    return $this->belongsToMany('App\User', 'follower_following', 'follower_id', 'following_id')
        ->select('id', 'username', 'name','uid');
}

action method: (if you have page and other object ... It's better to have this method in an trait)动作方法:(如果你有页面和其他对象......最好在特征中使用这个方法)

  public function action($id, Request $request)
{

    // $user 




    switch ($request->get('act')) {
        case "follow":
            $user->following()->attach($id);
            //response {"status":true}
            break;
        case "unfollow":
            $user->following()->detach($id);
            //response {"status":true}
            break;
        default:
            //response {"status":false, "error" : ['wrong act']}
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM