vainway 's avatar

pagination is affecting my foreach records

when I use pagination I lose the other records that are being retrieved from the database ex: on the first page I can see all the records but when I go on the next http://127.0.0.1:8000/irrigation?page=2 the records get lost.

app.blade.php these records below are the ones that are getting lost

<div class="col-lg-3">
					<div class="widget">
						<h5 class="widgetheading">Latest posts</h5>
						<ul class="link-list">
							@foreach($construction as $constructions)
							<li><a href="/constructions/{{ $constructions->id }}">{{ $constructions->title }}</a></li>
							@endforeach

							@foreach($bicycle as $bicycles)
							<li><a href="/bicycles/{{ $bicycles->id }}">{{ $bicycles->title }}</a></li>
							@endforeach

							@foreach($marshland as $marshlands)
							<li><a href="/marshlands/{{ $marshlands->id }}">{{ $marshlands->title }}</a></li>
							@endforeach

							@foreach($irrigation as $irrigations)
							<li><a href="/irrigation/{{ $irrigations->id }}">{{ $irrigations->title }}</a></li>
							@endforeach
						</ul>
					</div>
				</div>

irrigation_ps.blade.php

<div class="container">
		<div class="row">			
			<div class="col-md-10">
				<div class="panel panel-default">
					<div class="panel-heading">
						<strong>Related Posts</strong>
					</div>
					@foreach($irrigation_ps as $irrigation_pst)
					<div class="panel-body">
						<div class="media">
							<a class="media-left" href="#">
								<img src="{{ Storage::url($irrigation_pst->slug) }}" width="300" height="100%" alt="">
							</a>
							<div class="media-body">
								<h4 class="media-heading">{{ $irrigation_pst->title }}</h4>
								<p>{{ $irrigation_pst->litle_desc }}</p>
								<div class="ficon">
									<a href="/irrigation/{{ $irrigation_pst->id }}" alt="">Learn more</a> <i class="fa fa-long-arrow-right"></i>
								</div>
							</div>
						</div>
					</div>
					@endforeach
				</div>			
			</div>				
		</div>
	</div>
	
	<div class="container">
		<nav>
		{{ $irrigation_ps->links('pagination') }}
		</nav>
	</div>

this my controller

public function index()
    {
        $irrigation_ps = Irrigation::OrderBy('id', 'desc')->paginate(1);
        $footers = Footer::OrderBy('id', 'desc')->paginate(1);

        // our services
        $construction = Contruction::OrderBy('id', 'desc')->paginate(1);
        $bicycle = Bicycle::OrderBy('id', 'desc')->paginate(1);
        $marshland = Marshland::OrderBy('id', 'desc')->paginate(1);
        $irrigation = Irrigation::OrderBy('id', 'desc')->paginate(1);
        return view('project.pages.irrigation_ps', [
            'irrigation_ps' => $irrigation_ps,
            'footers' => $footers,

            //our services
            'construction' => $construction,
            'bicycle' => $bicycle,
            'marshland' => $marshland,
            'irrigation' => $irrigation,
        ]);
    }

I don't know where the problem is and why they get lost am using laravel 8 but in laravel 6 I had no error like this please explain to me

0 likes
9 replies
geraintp's avatar
        $irrigation_ps = Irrigation::OrderBy('id', 'desc')->paginate(1);
        $footers = Footer::OrderBy('id', 'desc')->paginate(1);

        // our services
        $construction = Contruction::OrderBy('id', 'desc')->paginate(1);
        $bicycle = Bicycle::OrderBy('id', 'desc')->paginate(1);
        $marshland = Marshland::OrderBy('id', 'desc')->paginate(1);
        $irrigation = Irrigation::OrderBy('id', 'desc')->paginate(1);

your paginating all your collections, all of them will detect and use the page=2 param not just your irrigations collection, so in essence, you're seeing page 2 of all your collections? that help?

and your per page is being set to 1, so each foreach loop will only show one result

Snapey's avatar

how does return view('alabetek.pages.irrigation_ps' relate to index.php?

vainway 's avatar

@Snapey I mistakenly wrote index.php I should have written irrigation_ps.blade.php

vainway 's avatar

@Snapey thanks man it worked, I appreciate you so much you always give me great answers to all the questions I asked here if possible one day I could buy a drink.

vainway 's avatar

@MichalOravec thanks you too bruh even though their codes were a little bit confusing @snapey 's way was much clear for me to match mine, finally found that they were both collect, peace

MichalOravec's avatar

@niyo Maybe if you spend more time with learning than asking questions in the forum more things will be clear to you. You have just 3 lessons completed.

Please or to participate in this conversation.