{{ $user->name }}
Joined {{ $user->created_at->diffForHumans() }}
diff --git a/app/Followable.php b/app/Followable.php new file mode 100644 index 0000000..2773961 --- /dev/null +++ b/app/Followable.php @@ -0,0 +1,46 @@ +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' + ); + } +} diff --git a/app/Http/Controllers/FollowsController.php b/app/Http/Controllers/FollowsController.php new file mode 100644 index 0000000..4908480 --- /dev/null +++ b/app/Http/Controllers/FollowsController.php @@ -0,0 +1,17 @@ +user() + ->toggleFollow($user); + + return back(); + } +} diff --git a/app/Http/Controllers/TweetsController.php b/app/Http/Controllers/TweetsController.php index 44de87c..f882423 100644 --- a/app/Http/Controllers/TweetsController.php +++ b/app/Http/Controllers/TweetsController.php @@ -9,7 +9,9 @@ class TweetsController extends Controller public function index() { return view('tweets.index', [ - 'tweets' => auth()->user()->timeline() + 'tweets' => auth() + ->user() + ->timeline() ]); } @@ -24,6 +26,6 @@ public function store() 'body' => $attributes['body'] ]); - return redirect('/home'); + return redirect()->route('home'); } } diff --git a/app/User.php b/app/User.php index 76a9e55..05b36ee 100644 --- a/app/User.php +++ b/app/User.php @@ -7,25 +7,21 @@ class User extends Authenticatable { - use Notifiable; + use Notifiable, Followable; /** * The attributes that are mass assignable. * * @var array */ - protected $fillable = [ - 'name', 'email', 'password', - ]; + protected $fillable = ['name', 'email', 'password']; /** * The attributes that should be hidden for arrays. * * @var array */ - protected $hidden = [ - 'password', 'remember_token', - ]; + protected $hidden = ['password', 'remember_token']; /** * The attributes that should be cast to native types. @@ -33,12 +29,12 @@ class User extends Authenticatable * @var array */ protected $casts = [ - 'email_verified_at' => 'datetime', + 'email_verified_at' => 'datetime' ]; public function getAvatarAttribute() { - return "https://i.pravatar.cc/200?u=".$this->email; + return "https://i.pravatar.cc/200?u=" . $this->email; } public function timeline() @@ -47,30 +43,17 @@ public function timeline() return Tweet::whereIn('user_id', $friends) ->orWhere('user_id', $this->id) - ->latest()->get(); + ->latest() + ->get(); } public function tweets() { - return $this->hasMany(Tweet::class); - } - - public function follow(User $user) - { - return $this->follows()->save($user); - } - - public function follows() - { - return $this->belongsToMany( - User::class, - 'follows', - 'user_id', 'following_user_id' - ); + return $this->hasMany(Tweet::class)->latest(); } - public function getRouteKeyName() + public function path() { - return 'name'; + return route('profile', $this->name); } } diff --git a/resources/views/_friends-list.blade.php b/resources/views/_friends-list.blade.php index 28890fc..171b520 100644 --- a/resources/views/_friends-list.blade.php +++ b/resources/views/_friends-list.blade.php @@ -5,7 +5,7 @@ @foreach (auth()->user()->follows as $user)
Joined {{ $user->created_at->diffForHumans() }}