简体   繁体   中英

Where clause in eloquent relation in laravel

I have two models Student and Fees which has relations

  • Student has many Fees
  • Fees belongsTo Student

In Student.php

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

In Fees.php

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

Now I need to Find Fees for individual students. What should I do now?

In controller

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

it's not working...

Try this

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

If you are trying to get students with fees then you can do it this way

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

Modified code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM