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
1 parent
e92d3b4
commit 36dc366
Showing
11 changed files
with
120 additions
and
57 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,46 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
trait Followable | ||
{ | ||
public function follow(User $user) | ||
{ | ||
return $this->follows()->save($user); | ||
} | ||
|
||
public function unfollow(User $user) | ||
{ | ||
return $this->follows()->detach($user); | ||
} | ||
|
||
public function toggleFollow(User $user) | ||
{ | ||
// Tip: You can also use the toggle() method. | ||
// We'll cover this in the next episode. | ||
// $this->follows()->toggle($user); | ||
|
||
if ($this->following($user)) { | ||
return $this->unfollow($user); | ||
} | ||
|
||
return $this->follow($user); | ||
} | ||
|
||
public function following(User $user) | ||
{ | ||
return $this->follows() | ||
->where('following_user_id', $user->id) | ||
->exists(); | ||
} | ||
|
||
public function follows() | ||
{ | ||
return $this->belongsToMany( | ||
User::class, | ||
'follows', | ||
'user_id', | ||
'following_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\User; | ||
|
||
class FollowsController extends Controller | ||
{ | ||
public function store(User $user) | ||
{ | ||
auth() | ||
->user() | ||
->toggleFollow($user); | ||
|
||
return back(); | ||
} | ||
} |
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
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
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,11 @@ | ||
<form method="POST" | ||
action="/profiles/{{ $user->name }}/follow" | ||
> | ||
@csrf | ||
|
||
<button type="submit" | ||
class="bg-blue-500 rounded-full shadow py-2 px-4 text-white text-xs" | ||
> | ||
{{ auth()->user()->following($user) ? 'Unfollow Me' : 'Follow Me' }} | ||
</button> | ||
</form> |
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