Вот мои коды:
UserController.php:
Код: Выделить всё
private function getSharedData($user)
{
$currentlyFollowing = 0;
if (auth()->check()) {
$currentlyFollowing = Follow::where([['user_id', '=', auth()->user()->id], ['followeduser', '=', $user->id]])->count();
}
View::share('sharedData', ['currentlyFollowing' => $currentlyFollowing, 'avatar' => $user->avatar, 'username' => $user->username, 'postCount' => $user->posts()->count()]);
}
public function profile(User $user)
{
$this->getSharedData($user);
return view('profile-posts', ['posts' => $user->posts()->latest()->get()]);
}
public function profileFollowers(User $user)
{
$this->getSharedData($user);
$followers = $user->followers()->latest()->get();
return view('profile-posts', compact('followers'));
}
public function profileFollowing(User $user)
{
$this->getSharedData($user);
$following = $user->followingTheseUsers()->latest()->get();
return view('profile-posts', compact('following'));
}
Код: Выделить всё
public function posts(){
return $this->hasMany(Post::class, 'user_id');
}
public function followers() {
return $this->hasMany(Follow::class, 'followeduser');
}
public function followingTheseUsers() {
return $this->hasMany(Follow::class, 'user_id');
}
Код: Выделить всё
public function userDoingTheFollowing() {
return $this->belongsTo(User::class, 'user_id');
}
public function userBeingFollowed() {
return $this->belongsTo(User::class, 'followeduser');
}
Код: Выделить всё
@foreach($followers as $follow)
[url=/profile/{{$follow-]userDoingTheFollowing->username}}" class="list-group-item list-group-item-action">
[img]{{$follow-[/img]
userDoingTheFollowing->avatar}}" />
{{$follow->userDoingTheFollowing->username}}
[/url]
@endforeach
@foreach($followers as $follow)
[url=/profile/{{$follow-]userDoingTheFollowing->username}}" class="list-group-item list-group-item-action">
[img]{{$follow-[/img]
userDoingTheFollowing->avatar}}" />
{{$follow->userDoingTheFollowing->username}}
[/url]
@endforeach
Код: Выделить всё
Route:: get('/profile-posts', [UserController::class, 'profileFollowers']);
Route:: get('/profile-posts', [UserController::class, 'profileFollowing']);
Код: Выделить всё
$this->getSharedData($user);
return view('profile-posts', ['followers' => $user->followers()->latest()->get()]);
Подробнее здесь: https://stackoverflow.com/questions/784 ... laravel-11
Мобильная версия