Deekshith's avatar

Add pagination to one to many relationsship

i have a query like below,

$testdetail = Test::with(['offlineconvertedtests.userdet.userregistereddetail' => function($q) use($coursedetails) {
            $q->where('course_id',$coursedetails->course_id)
              ->whereNotNull('registration_number');
        }])->with(['offlineconvertedtests.roomslotdet'])->where('test_code',$testcode)->where('course',$coursedetails->course_id)->first();

i tried to add pagination like below,

$convertedtests = $testdetail->offlineconvertedtests()->paginate(30);

but i am not getting all relationship data. (Example: i am not getting offlineconvertedtests.roomslotdet data How to apply pagination ?

0 likes
3 replies
Tray2's avatar

I'm not sure you can do it that way.

I take it you are talking about a show view here?

If so I would first fetch the master into one variable and then all the children.

Something like this.

public function(Author $author)
{
	$books = Book::where('author_id', $author->id)->paginate(20);
	return view('authors.show')
		->with([
		'author' => $author,
		'books' => $books
	]);
} 
Deekshith's avatar

@Tray2 Thank you for the reply here it is one to many relation i have to apply pagination for relationship data i tired like below,

$convertedtests = $testdetail->offlineconvertedtests()->with(['userdet.userregistereddetail' => function($q) use($coursedetails) {
            $q->where('course_id',$coursedetails->course_id)
              ->whereNotNull('registration_number');
        }])->paginate(30);

this one is working fine. can you please check this and confirm if it s right way to do that?

Please or to participate in this conversation.