我有两个桌子
**students :**
id
family_id
name
**family :**
id
father_name
father_civil_id
contact_no
两个表都是使用family_id连接的,我想用口才说出每个学生有多少个兄弟姐妹。 您能用控制器/模型/视图来帮助我吗?
答案 0 :(得分:1)
假设您有一个学生和家庭模型。然后,您有几种方法可以做到这一点。
以下是您提供的信息中我最容易猜到的两个。
控制器
Student::where('family_id', $family_id)->get();
家庭模式
class Family extends Model
{
// Since Laravel will expect your table to be 'families'
protected $table = 'family';
public function students()
{
return $this->hasMany(Student::class);
}
}
控制器
$family = Family::with('students')->inRandomOrder()->first();
$siblings = $family->students;
答案 1 :(得分:0)
在学生模型中:
public function family() {
return $this->belongsTo('App\Models\Family');
}
public function getSiblings() {
return $this->family->students;
}
因此您可以由学生来称呼它
$student->getSiblings();