4

Student.php

It has many fees

class student extends Model{
    public function fees($round){
        return $this->hasMany(fee::class)->where('payment_round','=',$round);
    }
}

Fee.blade.php

@foreach($student->fees("parameter") as $fee)
    {{$fee->myproperty}}
@endforeach

How I can pass a parameter to $student->fees("parameter")?

Community
  • 1
  • 1
Aman
  • 2,316
  • 3
  • 17
  • 23
  • 1
    Why don't you give a try and share us what error you're getting? e.g `$var = 'Hello'; @foreach($student->fees($var) as $fee) {{$fee->myproperty}} @endforeach` – Iftikhar uddin Feb 24 '19 at 17:05
  • It doesn't show any error message. It is just empty – Aman Feb 24 '19 at 17:12

2 Answers2

2

You can keep relationship as simple as possible and add a second method which uses that to get the data you want :

<?php

class student extends Model{

    public function fees(){
        return $this->hasMany(fee::class);
    }

    public function getFeeByRound($round)
    {
        return $this->fees()->where('payment_round','=',$round)->get();
    }

}

And then you can use this as :

@foreach($student->getFeeByRound($parameter) as $fee)
   {{ $fee->myproperty }}
@endforeach
Mihir Bhende
  • 8,677
  • 1
  • 30
  • 37
1

Try something like that:

@foreach($student->fees($parameter)->get() as $fee)
   {{ $fee->myproperty }}
@endforeach

I think you had confused the collection of related models fees with the query constructor fees().

dparoli
  • 8,891
  • 1
  • 30
  • 38