Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vipin93's avatar
Level 13

How to use paginate with relationship?

I want to use paginate. Like a user have many subscriptions and then show all subscriptions in his account using using foreach but problem is that how can paginate them here is my controller

public function fdetail($reg_no = null)
   {
      $s = Student::where('reg_no', $reg_no)->with('subscriptions')->first();

      return view('fee.invoice_deatil',compact('s'));
   }

my Subscription model

public function students()
    {
        return $this->belongsTo(Student::Class,'student_id');
    }

my Student model

 public function subscriptions()
    {
        return $this->hasMany(Subscription::Class)->latest();
    }

my view

@foreach($s->subscriptions as $invoi)
           
            <tbody>
              <tr>
                  <td>{{$invoi->id }}</td>
                  <td>{{ $invoi->created_at->format('d/m/Y') }}</td>
                  <td>{{ $invoi->month->format('F') }}</td>
               </tr>
                </tbody>
@endforeach 
0 likes
17 replies
fraserk's avatar
fraserk
Best Answer
Level 16

You can try something like

 Subsciption::whereHas('Student'=>function($query) use($reg_no){
    $query->where('reg_no', $reg_no);
})->paginate(10);
1 like
vipin93's avatar
Level 13

it is possible with revers $sub = Subscription::where('students', function($query)use ($reg){ $query->where('reg_no',$reg); })->paginate(10);

fraserk's avatar

Yes that should work. See my example above.

vipin93's avatar
Level 13

@fraserk

$invoces =  Subscription::whereHas('students',function($query) use($reg_no){

             $query->where('reg_no', $reg_no)->first();

        })->paginate(12);

errors

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'subscriptions.student_id' in 'where clause' (SQL: select * from `students` where `subscriptions`.`student_id` = `students`.`id` and `reg_no` = GRN2017985534 limit 1)
fraserk's avatar

Does the subscriptions table have the student_id field?

vipin93's avatar
Level 13

yes

Schema::create('subscriptions', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('student_id')->unsigned()->index();
            $table->integer('course_id')->unsigned()->index();
            $table->string('other_fee')->nullable();
            $table->string('tution_fee');
            $table->string('late_fee')->nullable();
            $table->string('development_fee')->nullable();
            $table->string('remarks')->nullable();
            $table->date('month');
            $table->timestamps();
SaeedPrez's avatar

You shouldn't use ->first(), ->get(), etc in the whereHas()..

$invoces =  Subscription::whereHas('students',function($query) use($reg_no){
    $query->where('reg_no', $reg_no);
})->paginate(12);
SaeedPrez's avatar

@vipin93 Glad I could help ☺ Give the best answer reward to @fraserk, he did all the hard work, I just fixed a tiny error in his code.

vipin93's avatar
Level 13

@SaeedPrez i was thinking too @fraserk deserved best answered but if someone will see this questioned maybe he got that errors

fraserk's avatar

lol.. It's fine. @SaeedPrez had the correct answer, with a little extra helpful info. It will help someone in the future.

1 like
SaeedPrez's avatar

@fraserk credit should go where credit is due ☺ I didn't want to jump in but then I noticed you guys were going a bit off-track.

vipin93's avatar
Level 13

and @fraserk edit your answered.

whereHas('students',function($query) use($reg_no){ 

Please or to participate in this conversation.