【问题标题】:Where clause in eloquent relation in laravelLaravel中雄辩关系中的Where子句
【发布时间】:2021-01-14 05:17:18
【问题描述】:

我有两个模型 Student 和 Fees 有关系

  • 学生有很多费用
  • 费用属于学生

Student.php

public function fees()
{
    return $this->hasMany(Fees::class, 'std_id');
}

Fees.php

public function student(){
    return $this->belongsTo(Student::class,'std_id');
}

现在我需要为个别学生查找费用。我现在该怎么办?

在控制器中

$fees = Fees::with('student')->where('fees->student->id', '=', $request->id)->get();

它不工作......

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    试试这个

    $student_fees = Student::find($request->id)->fees;
    

    【讨论】:

      【解决方案2】:

      如果你想让学生付费,那么你可以这样做

      $students = Student::with('fees')->where('std_id','=',$request->id)->get(); //also can get it by single instance using ->first();
      

      修改后的代码

      $fees = Fees::with('student')->where('std_id', '=', $request->id)->get();
      

      【讨论】:

      • 它会产生另一个问题。这就是我以这种方式找到解决方案的原因
      • 您能进一步解释一下吗?
      • 更新了我上面的评论也许对你有帮助
      猜你喜欢
      • 2018-09-03
      • 2020-12-15
      • 2015-05-17
      • 2014-11-05
      • 1970-01-01
      • 2018-05-19
      • 2021-02-13
      • 2018-11-12
      • 1970-01-01
      相关资源
      最近更新 更多