我试图从我的链接表中获取数据,它由学生表中的student_id和M_fees表中的mfee_id组成。 我的模特是:
Student:
class student extends Model
{
//
protected $fillable = ['first_name','middle_name','last_name','address','contact','dob','grade_id','status','scholorship','admission_year','passout_year'];
public function grade() {
return $this->belongsTo(Grade::class);
}
public function M_Fees() {
return $this->belongsToMany('App\M_Fees');
}
}
M_fees:
class M_fees extends Model
{
protected $fillable = ['fee_type','amount'];
public function Student()
{
return $this->belongsToMany('App\Student');
}
}
现在,我如何检索特定学生的M_fees' fee_type和amount)? 我在我的控制器中使用了以下内容:
$student=Student::all()->whereLoose('id',$sid);
foreach ($student->M_fees as $M_fees) {
echo $M_fees->pivot->fee_type;
}
但它似乎没有起作用。
任何人都可以帮助我吗?
答案 0 :(得分:0)
兄弟试试这个。有时候你需要定义数据透视表的名称。希望能帮忙。让我知道PLZ。
学生:
class student extends Model
{
protected $table = "table_name";
protected $fillable = ['first_name','middle_name','last_name','address','contact','dob','grade_id','status','scholorship','admission_year','passout_year'];
public function grade() {
return $this->belongsTo(Grade::class);
}
public function M_Fees() {
return $this->belongsToMany('App\M_Fees','sfees');
}
}
M_fees
class M_fees extends Model
{
protected $table = "table_name";
protected $fillable = ['fee_type','amount'];
public function Student()
{
return $this->belongsToMany('App\Student','sfees');
}
}
兄弟我认为你的循环有问题。你必须循环通过学生和每个学生你必须取费,金额。这个。
兄弟我认为你的循环有问题。你必须循环通过学生和每个学生你必须取费,金额。这个。
foreach ($student as $single_student) {
foreach ($single_student->M_fees as $M_fees) {
echo $M_fees->pivot->fee_type;
}
}
希望它能奏效。我知道