Answer the question
In order to leave comments, you need to log in
Why are there duplicate requests?
Continuing to deal with the question How to get authors (users) using eager lazy loading? . There was a problem with pagination. As I understand it, I didn’t manage to do pagination correctly. Has anyone encountered the same problem and how to fix it, if of course it can be done?
Controller (standard):
/* Страница профиля */
public function getAccount(string $login = null){
$withUser = User::withUser($login);
if($withUser){
return view('Account.getAccount', ['user' => $withUser]);
}
return abort(404);
}
class User extends Authenticatable{
use Notifiable;
protected $table = 'users';
protected $primaryKey = 'id_users';
/* Получаем все зависимости */
public static function withUser(string $login){
$user = User::where('login_users', $login)->first(); // Получаем данные о пользователе
if($user){
$user->load(['getUserPosts' => function($posts){
$posts->orderBy('id_posts');
}]);
$user->load(['getUserVideo' => function($videos){
$videos->orderBy('id_videos');
}]);
}
return $user; // Отдаём эти данные
}
/* Получаем все посты со страницы пользователя */
public function getUserPosts(){
return $this->hasMany(Posts::class, 'author_posts', 'id_users');
}
/* Получаем список видео у пользователя */
public function getUserVideo(){
return $this->hasMany(Video::class, 'author_videos', 'id_users');
}
}
<div class="card-panel">
<ul class="tabs">
<li class="tab col s6">
<a href="#posts">Посты
<span class="new badge" data-badge-caption="">
{{ $user->getUserPosts->count() }}
</span>
</a>
</li>
<li class="tab col s6">
<a href="#video">Видео
<span class="new badge" data-badge-caption="">
{{ $user->getUserVideo->count() }}
</span>
</a>
</li>
</ul>
</div>
@forelse($user->getUserPosts()->paginate(3) as $posts)
<div class="card horizontal">
<div class="card-image">
<img src="https://semantic-ui.com/images/avatar2/large/kristy.png">
</div>
<div class="card-stacked">
<div class="card-content">
<div class="chips">
<div class="chip">
<img src="https://semantic-ui.com/images/avatar2/large/kristy.png" alt="Contact Person">
{{ $user->login_users }}
</div>
<div class="chip">
<i class="material-icons small">access_time</i>
{{ LocalizedCarbon::instance($posts->created_at)->diffForHumans() }}
</div>
</div>
<p>{{ $posts->text_posts }}</p>
</div>
</div>
</div>
<div class="ui divider"></div>
@empty
Нет постов
@endforelse
{{ $user->getUserPosts()->paginate(3)->links() }}
@forelse($user->getUserVideo as $video)
<div class="col s6">
<div class="card">
<div class="card-image">
<a href="{{ URL::route('Videos.getVideoID', $video->id_videos) }}">
<img src="http://neurogadget.net/wp-content/uploads/2016/12/Minecraft-Pocket-Edition.png">
</a>
<span class="card-title">{{ $video->title_videos }}</span>
@if(!$video->stream_videos)
<a href="{{ URL::route('Videos.getVideos') }}" class="btn-floating halfway-fab waves-effect waves-light blue tooltipped left" data-position="top" data-delay="50" data-tooltip="Видеозапись">
<i class="material-icons">play_arrow</i>
</a>
@endif
@if($video->stream_videos)
<a href="{{ URL::route('Videos.getVideos') }}" class="btn-floating pulse halfway-fab waves-effect waves-light red tooltipped" data-position="top" data-delay="50" data-tooltip="Трансляция">
<i class="material-icons">play_arrow</i>
</a>
@endif
</div>
{{--<div class="card-action">--}}
{{--<a href="#">This is a link</a>--}}
{{--</div>--}}
</div>
</div>
@empty
Нет видео
@endforelse
Answer the question
In order to leave comments, you need to log in
You must pass data to the view using the paginate($count) method
Example from the documentation
public function index()
{
$users = DB::table('users')->paginate(15);
return view('user.index', ['users' => $users]);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question