一、hasMany模式
1、hasMany模式,适合主表关联附表,适合一堆多查询,具体设置方式如下
return $this->hasMany(stumenu::class,'外键','主键');
2、使用stumenu()方法模式可以进一步进行数据的筛选
$user->stumenu()->where('id','>',1)->select();
3、使用has()方法查询关联附表的主表内容,比如大于等于2条的主表记录
Students::has('stumenu',['uname'=>1]->select())
4、使用hasWhere()方法,查询关联附表筛选后记录
Students::hasWhere('stumenu',['sex'=>'男'])->select();
5、使用save()方法和saveAll()进行关联新增和批量关联新增
$user = UserModel::find(19);
$user->profile()->save(['hobby'=>'测试喜好', 'status'=>1]);
$user->profile()->saveAll([
['hobby'=>'测试喜好', 'status'=>1],
['hobby'=>'测试喜好', 'status'=>1]
]);
6、使用together()方法可以删除主表内容时,将附表关联的内容全部删除
$user=Students::with('stumenu')->find(227);
$user->together(['stumenu'])->delete();