Skip to content

Commit a288fb8

Browse files
committed
使用多对多关联,完成用户关注与取关操作
1 parent e64fa4a commit a288fb8

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

app/Http/Controllers/UserController.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ public function store(Request $request)
7070
return redirect()->route('home')->with('success', '注册成功!并已为你自动登录~~~');
7171
}
7272

73+
/**
74+
* 关注与取关
75+
* @param User $user
76+
* @return \Illuminate\Http\RedirectResponse
77+
*/
78+
public function follow(User $user)
79+
{
80+
$user->followToggle(\Auth::user()->id);
81+
82+
return back();
83+
}
84+
7385
/**
7486
* 用户个人主页
7587
*
@@ -79,7 +91,14 @@ public function store(Request $request)
7991
public function show(User $user)
8092
{
8193
$blogs = Blog::where('user_id', $user->id)->orderBy('id', 'DESC')->paginate(10);
82-
return view('user.show', compact('user', 'blogs'));
94+
95+
// 关注状态
96+
$followStatus = '关注我';
97+
if (\Auth::check()) {
98+
$followStatus = $user->isFollow(\Auth::user()->id) ? '取消关注' : '关注我';
99+
}
100+
101+
return view('user.show', compact('user', 'blogs', 'followStatus'));
83102
}
84103

85104
/**

app/User.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,46 @@ public function blogs()
3535
{
3636
return $this->hasMany(Blog::class);
3737
}
38+
39+
/**
40+
* 我的关注
41+
*/
42+
public function follows()
43+
{
44+
return $this->belongsToMany(User::class, 'follows', 'fans', 'user_id')->withTimestamps();
45+
}
46+
47+
/**
48+
* 我的粉丝
49+
*
50+
* 点击关注,往user_id里面插入被关注者id,在fans插入我的id
51+
*
52+
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
53+
*/
54+
public function fans()
55+
{
56+
return $this->belongsToMany(User::class, 'follows', 'user_id', 'fans')->withTimestamps();
57+
}
58+
59+
/**
60+
* 查询指定用户是否是我的粉丝
61+
*
62+
* @param $uid
63+
* @return mixed
64+
*/
65+
public function isFollow($uid)
66+
{
67+
return $this->fans()->wherePivot('fans', $uid)->first();
68+
}
69+
70+
/**
71+
* 关注与取关
72+
*
73+
* @return array
74+
*/
75+
public function followToggle($ids)
76+
{
77+
$ids = is_array($ids) ?: [$ids];
78+
return $this->fans()->toggle($ids);
79+
}
3880
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateFollowsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('follows', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->integer('user_id');
19+
$table->integer('fans');
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::dropIfExists('follows');
32+
}
33+
}

resources/views/user/show.blade.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
<div class="card">
55
<div class="card-header">
66
<h1 class="text-center">{{$user['name']}}</h1>
7+
<div class="text-center">
8+
<a href="" class="btn btn-danger">我的关注:{{$user->follows()->count()}}</a>
9+
<a href="" class="btn btn-dark">我的粉丝:{{$user->fans()->count()}}</a>
10+
<a class="btn btn-success" href="{{ route('userFollow', $user) }}" role="button">{{ $followStatus }}</a>
11+
</div>
712
</div>
813
<div class="card-body">
914
<table class="table">

routes/web.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@
2929
Route::post('findPassword', 'PasswordController@findPassword')->name('findPassword');
3030

3131
Route::get('resetPassword/{token}', 'PasswordController@resetPasswordForm')->name('resetPassword');
32-
Route::post('resetPassword', 'PasswordController@resetPassword')->name('resetPasswordStore');
32+
Route::post('resetPassword', 'PasswordController@resetPassword')->name('resetPasswordStore');
33+
34+
// 关注与取关
35+
Route::get('follow/{user}', 'UserController@follow')->name('userFollow');
36+
37+
// 我的粉丝
38+
39+
// 我关注的

0 commit comments

Comments
 (0)