namespace app\admin\model;
use think\Model;
class Teachers extends Model
{
public function students(){
return $this->belongsToMany('Students','s_t','s_id','t_id');
}
}
students.php:
namespace app\admin\model;
use think\Model;
class Students extends Model
{
public function teachers(){
return $this->belongsToMany('Teachers','s_t','t_id','s_id');
}
}
teachers表:id,name,gender; students表:id,name,gender,hobby; 中间表:id,s_id,t_id;
index.php:
$s = Students::get(1);
$s->teachers()->save(['id'=>14,'name'=>'god','gender'=>0]);
运行结果,teachers表有添加1条记录,但中间表没有,而且提示:缺少关联表数据, 求大神帮帮我!!
最佳答案