Skip to content

Commit

Permalink
Explore Page episode complete
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Apr 8, 2020
1 parent 1b0131f commit 13806d6
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 23 deletions.
10 changes: 1 addition & 9 deletions app/Followable.php
Expand Up @@ -16,15 +16,7 @@ public function unfollow(User $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);
$this->follows()->toggle($user);
}

public function following(User $user)
Expand Down
15 changes: 15 additions & 0 deletions app/Http/Controllers/ExploreController.php
@@ -0,0 +1,15 @@
<?php

namespace App\Http\Controllers;

use App\User;

class ExploreController extends Controller
{
public function index()
{
return view('explore', [
'users' => User::paginate(50),
]);
}
}
6 changes: 4 additions & 2 deletions app/Http/Controllers/ProfilesController.php
Expand Up @@ -28,7 +28,7 @@ public function update(User $user)
Rule::unique('users')->ignore($user),
],
'name' => ['string', 'required', 'max:255'],
'avatar' => ['required', 'file'],
'avatar' => ['image'],
'email' => [
'string',
'required',
Expand All @@ -45,7 +45,9 @@ public function update(User $user)
],
]);

$attributes['avatar'] = request('avatar')->store('avatars');
if (request('avatar')) {
$attributes['avatar'] = request('avatar')->store('avatars');
}

$user->update($attributes);

Expand Down
10 changes: 5 additions & 5 deletions app/User.php
Expand Up @@ -34,13 +34,13 @@ class User extends Authenticatable

public function getAvatarAttribute($value)
{
return asset($value);
return asset($value ?: '/images/default-avatar.jpeg');
}

// Next episode...
// public function setPasswordAttribute($value)
// {
// }
public function setPasswordAttribute($value)
{
$this->attributes['password'] = bcrypt($value);
}

public function timeline()
{
Expand Down
4 changes: 3 additions & 1 deletion database/factories/UserFactory.php
Expand Up @@ -19,10 +19,12 @@

$factory->define(User::class, function (Faker $faker) {
return [
'username' => $faker->unique()->username,
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'password' =>
'$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
});
Binary file added public/images/default-avatar.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion resources/views/components/follow-button.blade.php
@@ -1,6 +1,6 @@
@unless (current_user()->is($user))
<form method="POST"
action="/profiles/{{ $user->name }}/follow"
action="{{ route('follow', $user->username) }}"
>
@csrf

Expand Down
19 changes: 19 additions & 0 deletions resources/views/explore.blade.php
@@ -0,0 +1,19 @@
<x-app>
<div>
@foreach ($users as $user)
<a href="{{ $user->path() }}" class="flex items-center mb-5">
<img src="{{ $user->avatar }}"
alt="{{ $user->username }}'s avatar"
width="60"
class="mr-4 rounded"
>

<div>
<h4 class="font-bold">{{ '@' . $user->username }}</h4>
</div>
</a>
@endforeach

{{ $users->links() }}
</div>
</x-app>
2 changes: 1 addition & 1 deletion resources/views/profiles/edit.blade.php
Expand Up @@ -55,7 +55,7 @@
type="file"
name="avatar"
id="avatar"
required
accept="image/*"
>

<img src="{{ $user->avatar }}"
Expand Down
2 changes: 1 addition & 1 deletion resources/views/welcome.blade.php
Expand Up @@ -74,7 +74,7 @@

<div class="links">
@auth
<a href="{{ url('/home') }}">Home</a>
<a href="{{ url('/tweets') }}">Home</a>
@else
<a href="{{ route('login') }}">Login</a>
<a href="{{ route('register') }}">Register</a>
Expand Down
14 changes: 11 additions & 3 deletions routes/web.php
Expand Up @@ -21,14 +21,22 @@
Route::get('/tweets', 'TweetsController@index')->name('home');
Route::post('/tweets', 'TweetsController@store');

Route::post('/profiles/{user:username}/follow', 'FollowsController@store');
Route::post(
'/profiles/{user:username}/follow',
'FollowsController@store'
)->name('follow');

Route::get(
'/profiles/{user:username}/edit',
'ProfilesController@edit'
)->middleware('can:edit,user');

// Next episode, we'll add the necessary authorization middleware.
Route::patch('/profiles/{user:username}', 'ProfilesController@update');
Route::patch(
'/profiles/{user:username}',
'ProfilesController@update'
)->middleware('can:edit,user');

Route::get('/explore', 'ExploreController@index');
});

Route::get('/profiles/{user:username}', 'ProfilesController@show')->name(
Expand Down

0 comments on commit 13806d6

Please sign in to comment.