在laravel中创建多个模型,使其位于

在laravel中创建多个模型,使其位于,laravel,laravel-5,eloquent,Laravel,Laravel 5,Eloquent,我有两种型号,Service和Schedule 计划模型具有以下关系: 公共功能服务(){ 返回$this->belongtomany('App\Service'、'services\u has\u schedules'、'services\u Service\u id'、'schedules\u schedule\u id'); } 服务模型具有以下关系: public function schedule(){ 返回$this->belongsToMany('App\Schedule','s

我有两种型号,
Service
Schedule

计划
模型具有以下关系:

公共功能服务(){
返回$this->belongtomany('App\Service'、'services\u has\u schedules'、'services\u Service\u id'、'schedules\u schedule\u id');
}
服务
模型具有以下关系:

public function schedule(){
返回$this->belongsToMany('App\Schedule','services\u has\u schedules','services\u service\u id','schedules\u Schedule\u id');
}
在前端,这将向我发送数组
day[]
(日期名称),
init[]
(开放时间),
finit[]
(关闭时间)

在控制器中,我想知道如何创建多个
计划
sync()
服务


您可以使用
syncwithout detaching
保留现有记录并添加新记录

如果不想分离现有ID,可以使用 SyncWithout分离方法:

$user->roles()->syncwithout分离([1,2,3])


如果使用sync,您将在透视表中添加您在
$schedules
上发送的所有ID,但是如果您已经有数据与
Schedule
相关,则
$schedules
中所有缺失的ID都将从关系中删除,就像上面说的。根据测试,我必须使用foreach来完成这几天的工作,并在里面执行创建时间表的功能,而不是使用
sync()
,而是使用
attach()
,我将测试并分享发生的事情。。。