-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
404 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
database/migrations/2018_07_20_174615_create_followers_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.