如何在刀片内的Laravel关系函数中传递参数?

时间:2019-02-24 16:46:40

标签: php laravel-5 laravel-blade laravel-5.7

Student.php

收费很多

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

如何将参数传递给$ student-> fees(“ parameter”)?

2 个答案:

答案 0 :(得分:1)

尝试类似的事情:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="ch" />
<input type="text" id="myinp" />

我认为您已经将相关模型@foreach($student->fees($parameter)->get() as $fee) {{ $fee->myproperty }} @endforeach 与查询构造函数fees混淆了。

答案 1 :(得分:0)

您可以保持关系尽可能简单,并添加第二种方法来获取所需的数据:

<?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();
    }

}

然后您可以将其用作:

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