D
D
Dmitry Kuznetsov2017-10-26 14:35:46
MySQL
Dmitry Kuznetsov, 2017-10-26 14:35:46

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);
  }

Model User :
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');
  }
}

View :
These are tabs for tabs, which are also included via @include. It shows the total number of posts and videos of the user.
<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>

These are 2 tabs connected via @include. Here posts and videos are displayed, switched by tab (Data is loaded immediately together, i.e. without reloading the page and without AJAX)
@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

What is the best way to do this? At the same time, it is necessary to find out how many posts and videos the user has, to receive the posts and videos themselves from the user with pagination.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
anlamas, 2017-10-27
@anlamas

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]);
    }

In the view itself
In English Pagination
In Russian Pagination

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question