记录多对多关系 Laravel 8
Posted
技术标签:
【中文标题】记录多对多关系 Laravel 8【英文标题】:Record many-to-many relationships Laravel 8 【发布时间】:2021-08-21 20:59:48 【问题描述】:我有这2张与Many to Mutos相关的表,以及它们之间的数据透视表,我是这样正常记录的,但我不知道如何建立这些值之间的关系。
每家银行记录7条记录,每周7天,每天都有开、关时间。
如何使这种关系自动进行?
public function createShop()
$schedules = [
['start' => now(),'end' => now()],
['start' => now(),'end' => now()],
['start' => now(),'end' => now()],
['start' => now(),'end' => now()],
['start' => now(),'end' => now()],
['start' => now(),'end' => now()],
['start' => now(),'end' => now()],
];
$daysweeks = [
['name' => 'Segunda-Feira'],
['name' => 'Terça-Feira'],
['name' => 'Quarta-Feira'],
['name' => 'Quinta-Feira'],
['name' => 'Sexta-Feira'],
['name' => 'Sábado'],
['name' => 'Domingo'],
];
$schedule = Schedules::insert($schedules);
$daysweek = DaysWeek::insert($daysweeks);
$daysweek->schedule()->sync($schedule);
【问题讨论】:
定义自动。 【参考方案1】:我的问题代码也是如此
foreach ($daysweeks as $daysweek)
$getShopDaySchedule = ShopDaySchedule::create([
'shop_id' => $selectShop->id,
'enable' => 0
]);
$getDay = $getShopDaySchedule->daysWeeks()->create($daysweek);
$getDay->allSchedules()->create($schedules);
【讨论】:
以上是关于记录多对多关系 Laravel 8的主要内容,如果未能解决你的问题,请参考以下文章