从链接 table(多对多)中检索数据。 Laravel
Retrieving data from linking table (Many to Many). Laravel
我正在尝试从我的链接 table sfees 获取数据,它由 student_id 来自学生 table 和 mfee_id 来自 M_fees table。
我的模型是:
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');
}
}
我的 table sfees 结构如下:
现在,我如何检索特定学生的 M_fees 'fee_type 和数量)?
我在我的控制器中使用了以下内容:
$student=Student::all()->whereLoose('id',$sid);
foreach ($student->M_fees as $M_fees) {
echo $M_fees->pivot->fee_type;
}
但是好像没有效果。
谁能帮帮我?
兄弟试试this.Sometimes你需要定义枢轴tablename.Hope会help.Let我知道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');
}
}
兄弟,我认为你的循环有一个 problem.You 必须循环遍历学生并且对于每个学生你都必须收取费用,amount.try 这个。
兄弟,我认为你的循环有一个 problem.You 必须循环遍历学生并且对于每个学生你都必须收取费用,amount.try 这个。
foreach ($student as $single_student) {
foreach ($single_student->M_fees as $M_fees) {
echo $M_fees->pivot->fee_type;
}
}
希望它会work.let我知道
我正在尝试从我的链接 table sfees 获取数据,它由 student_id 来自学生 table 和 mfee_id 来自 M_fees table。 我的模型是:
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');
}
}
我的 table sfees 结构如下:
现在,我如何检索特定学生的 M_fees 'fee_type 和数量)? 我在我的控制器中使用了以下内容:
$student=Student::all()->whereLoose('id',$sid);
foreach ($student->M_fees as $M_fees) {
echo $M_fees->pivot->fee_type;
}
但是好像没有效果。
谁能帮帮我?
兄弟试试this.Sometimes你需要定义枢轴tablename.Hope会help.Let我知道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');
}
}
兄弟,我认为你的循环有一个 problem.You 必须循环遍历学生并且对于每个学生你都必须收取费用,amount.try 这个。
兄弟,我认为你的循环有一个 problem.You 必须循环遍历学生并且对于每个学生你都必须收取费用,amount.try 这个。
foreach ($student as $single_student) {
foreach ($single_student->M_fees as $M_fees) {
echo $M_fees->pivot->fee_type;
}
}
希望它会work.let我知道