Why are duplicate requests made?

  • 0
    I continue to deal with the question How to get authors (users) using impatient lazy loading? . The problem with pagination has caught on. As I understand it, I failed to do the pagination correctly. Has anyone faced the same problem and how to fix it, if of course you can do it?

    Controller (standard):
    /* Страница профиля */
    	public function getAccount(string $login = null){
    		$withUser = User::withUser($login);
    
    		if($withUser){
    			return view('Account.getAccount', ['user' => $withUser]);
    		}
    
    		return abort(404);
    	}
    < br />
    User Model :
    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. This 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. Posts and videos are displayed here, switchable by tab (Data is loaded immediately together, ie 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? In this case, it is necessary to find out how many posts and videos the user has, to receive the posts and videos from the user with pagination.
    MySQL Xavier Pacheco, Apr 6, 2020

  • 1 Answers
  • 0
    You must pass data in 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
    {{$ users- & gt; links ()}}

    English Pagination

    In Russian Page output
    Aiden Hammond

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!