From 36dc366e93776f7949357a49baa09da965fc8dc2 Mon Sep 17 00:00:00 2001 From: Jeffrey Way Date: Tue, 31 Mar 2020 20:58:52 -0400 Subject: [PATCH] Follow Form episode complete --- app/Followable.php | 46 +++++++++++++++++++ app/Http/Controllers/FollowsController.php | 17 +++++++ app/Http/Controllers/TweetsController.php | 6 ++- app/User.php | 37 ++++----------- resources/views/_friends-list.blade.php | 2 +- resources/views/_sidebar-links.blade.php | 2 +- resources/views/_tweet.blade.php | 6 +-- resources/views/components/app.blade.php | 2 +- .../views/components/follow-button.blade.php | 11 +++++ resources/views/profiles/show.blade.php | 44 +++++++++--------- routes/web.php | 4 +- 11 files changed, 120 insertions(+), 57 deletions(-) create mode 100644 app/Followable.php create mode 100644 app/Http/Controllers/FollowsController.php create mode 100644 resources/views/components/follow-button.blade.php 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)
  • - + Profile diff --git a/resources/views/_tweet.blade.php b/resources/views/_tweet.blade.php index 2922d79..0dd48e4 100644 --- a/resources/views/_tweet.blade.php +++ b/resources/views/_tweet.blade.php @@ -1,6 +1,6 @@ -
    +
    - +
    - + {{ $tweet->user->name }}
    diff --git a/resources/views/components/app.blade.php b/resources/views/components/app.blade.php index d4b499a..c28c039 100644 --- a/resources/views/components/app.blade.php +++ b/resources/views/components/app.blade.php @@ -1,7 +1,7 @@
    -
    +
    @include ('_sidebar-links')
    diff --git a/resources/views/components/follow-button.blade.php b/resources/views/components/follow-button.blade.php new file mode 100644 index 0000000..7ebeeec --- /dev/null +++ b/resources/views/components/follow-button.blade.php @@ -0,0 +1,11 @@ +
    + @csrf + + +
    diff --git a/resources/views/profiles/show.blade.php b/resources/views/profiles/show.blade.php index 1cccbee..d993b25 100644 --- a/resources/views/profiles/show.blade.php +++ b/resources/views/profiles/show.blade.php @@ -1,26 +1,33 @@
    - +
    + -
    + +
    + +

    {{ $user->name }}

    Joined {{ $user->created_at->diffForHumans() }}

    - @@ -31,12 +38,7 @@ class="bg-blue-500 rounded-full shadow py-2 px-4 text-white text-xs" and his catch phrase "Eh...What's up, doc?"

    - +
    @include ('_timeline', [ diff --git a/routes/web.php b/routes/web.php index be0d1b2..96bc41a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -20,8 +20,10 @@ Route::middleware('auth')->group(function () { Route::get('/tweets', 'TweetsController@index')->name('home'); Route::post('/tweets', 'TweetsController@store'); + + Route::post('/profiles/{user:name}/follow', 'FollowsController@store'); }); -Route::get('/profiles/{user}', 'ProfilesController@show')->name('profile'); +Route::get('/profiles/{user:name}', 'ProfilesController@show')->name('profile'); Auth::routes();