Skip to content

Commit

Permalink
Add user following
Browse files Browse the repository at this point in the history
  • Loading branch information
hackjie committed Jul 20, 2018
1 parent 74f0389 commit 9e7b5be
Show file tree
Hide file tree
Showing 13 changed files with 404 additions and 0 deletions.
42 changes: 42 additions & 0 deletions app/Http/Controllers/FollowersController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Models\User;
use Auth;

class FollowersController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}

public function store(User $user)
{
if (Auth::user()->id === $user->id) {
return redirect('/');
}

if (!Auth::user()->isFollowing($user->id)) {
Auth::user()->follow($user->id);
}

return redirect()->route('users.show', $user->id);
}

public function destroy(User $user)
{
if (Auth::user()->id === $user->id) {
return redirect('/');
}

if (Auth::user()->isFollowing($user->id)) {
Auth::user()->unfollow($user->id);
}

return redirect()->route('users.show', $user->id);
}
}
15 changes: 15 additions & 0 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,19 @@ public function update(UserRequest $request, ImageUploadHandler $uploader, User
$user->update($data);
return redirect()->route('users.show', $user->id)->with('success', '个人资料更新成功!');
}

// 粉丝和关注
public function followings(User $user)
{
$users = $user->followings()->paginate(30);
$title = '关注的人';
return view('users.show_follow', compact('users', 'title'));
}

public function followers(User $user)
{
$users = $user->followers()->paginate(30);
$title = '粉丝';
return view('users.show_follow', compact('users', 'title'));
}
}
38 changes: 38 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,42 @@ public function setAvatarAttribute($path)

$this->attributes['avatar'] = $path;
}

// -----
// 粉丝和关注
public function followers()
{
// 粉丝
return $this->belongsToMany(User::Class, 'followers', 'user_id', 'follower_id');
}

public function followings()
{
// 正在关注的
return $this->belongsToMany(User::Class, 'followers', 'follower_id', 'user_id');
}

public function follow($user_ids)
{
if (!is_array($user_ids)) {
$user_ids = compact('user_ids');
}
$this->followings()->sync($user_ids, false);
}

public function unfollow($user_ids)
{
if (!is_array($user_ids)) {
$user_ids = compact('user_ids');
}
$this->followings()->detach($user_ids);
}

public function isFollowing($user_id)
{
return $this->followings->contains($user_id);
}
// -----


}
33 changes: 33 additions & 0 deletions database/migrations/2018_07_20_174615_create_followers_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFollowersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('followers', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->index();
$table->integer('follower_id')->index();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('followers');
}
}
1 change: 1 addition & 0 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public function run()
$this->call(TopicsTableSeeder::class);
$this->call(ReplysTableSeeder::class);
$this->call(LinksTableSeeder::class);
$this->call(FollowersTableSeeder::class);
}
}
31 changes: 31 additions & 0 deletions database/seeds/FollowersTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Seeder;
use App\Models\User;

class FollowersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$users = User::all();
$user = $users->first();
$user_id = $user->id;

// 获取去除掉 ID 为 1 的所有用户 ID 数组
$followers = $users->slice(1);
$follower_ids = $followers->pluck('id')->toArray();

// 关注除了 1 号用户以外的所有用户
$user->follow($follower_ids);

// 除了 1 号用户以外的所有用户都来关注 1 号用户
foreach ($followers as $follower) {
$follower->follow($user_id);
}
}
}
94 changes: 94 additions & 0 deletions public/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -8356,6 +8356,42 @@ body {
font-family: Hiragino Sans GB, "Hiragino Sans GB", Helvetica, "Microsoft YaHei", Arial,sans-serif;
}

section {
overflow: auto;
}

/* typography */

h1,
h2,
h3,
h4,
h5,
h6 {
line-height: 1;
}

h1 {
font-size: 3em;
letter-spacing: -2px;
margin-bottom: 30px;
text-align: center;
}

h2 {
font-size: 1.2em;
letter-spacing: -1px;
margin-bottom: 30px;
text-align: center;
font-weight: normal;
color: #777;
}

p {
font-size: 1.1em;
line-height: 1.7em;
}

/* header */

.navbar-static-top {
Expand Down Expand Up @@ -9012,3 +9048,61 @@ body {
font-size: 0.9em;
}

/* Users index */

.users {
list-style: none;
margin: 0;
padding-left: 0;
}

.users li {
overflow: auto;
padding: 10px 0;
border-bottom: 1px solid #eeeeee;
}

.avatar {
float: left;
margin-right: 10px;
max-width: 50px;
border-radius: 50%;
}

.stats {
overflow: auto;
margin-top: 0;
padding: 0;
}

.stats a {
float: left;
padding: 0 10px;
text-align: center;
width: 50%;
border-left: 1px solid #eeeeee;
color: gray;
}

.stats a:first-child {
padding-left: 0;
border: 0;
}

.stats a:hover {
text-decoration: none;
color: #337ab7;
}

.stats strong {
display: block;
font-size: 1.2em;
color: black;
}

#follow_form button {
margin: 0 auto;
display: block;
margin-top: 25px;
}

85 changes: 85 additions & 0 deletions resources/assets/sass/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@ body {
font-family: Hiragino Sans GB, "Hiragino Sans GB", Helvetica, "Microsoft YaHei", Arial,sans-serif;
}

section {
overflow: auto;
}

/* typography */

h1, h2, h3, h4, h5, h6 {
line-height: 1;
}

h1 {
font-size: 3em;
letter-spacing: -2px;
margin-bottom: 30px;
text-align: center;
}

h2 {
font-size: 1.2em;
letter-spacing: -1px;
margin-bottom: 30px;
text-align: center;
font-weight: normal;
color: #777;
}

p {
font-size: 1.1em;
line-height: 1.7em;
}

/* header */

.navbar-static-top {
Expand Down Expand Up @@ -215,3 +246,57 @@ body {
}
}
}

/* Users index */

.users {
list-style: none;
margin: 0;
padding-left: 0;
li {
overflow: auto;
padding: 10px 0;
border-bottom: 1px solid $gray-lighter;
}
}

.avatar {
float: left;
margin-right: 10px;
max-width: 50px;
border-radius: 50%;
}

.stats {
overflow: auto;
margin-top: 0;
padding: 0;
a {
float: left;
padding: 0 10px;
text-align: center;
width: 50%;
border-left: 1px solid $gray-lighter;
color: gray;
&:first-child {
padding-left: 0;
border: 0;
}
&:hover {
text-decoration: none;
color: #337ab7;
}
}
strong {
display: block;
font-size: 1.2em;
color: black;
}
}

#follow_form button {
margin: 0 auto;
display: block;
margin-top: 25px;
}

16 changes: 16 additions & 0 deletions resources/views/users/_follow_form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@if ($user->id !== Auth::user()->id)
<div id="follow_form">
@if (Auth::user()->isFollowing($user->id))
<form action="{{ route('followers.destroy', $user->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-sm">取消关注</button>
</form>
@else
<form action="{{ route('followers.store', $user->id) }}" method="post">
{{ csrf_field() }}
<button type="submit" class="btn btn-sm btn-primary">关注</button>
</form>
@endif
</div>
@endif
Loading

0 comments on commit 9e7b5be

Please sign in to comment.