以下只是一个例子:将以下两个模型与他们的关系:
User => hasMany('Address', 'user_id')
Address => => belongsTo('User', 'user_id')
我希望能够将用户直接保存地址与用户链接起来。 类似的东西:
$user = new User();
$user->name = "John";
$address1 = new Address();
$address1->street = "Foo street";
$address2 = new Address();
$address2->street = "Bar street";
$user->addresses()->push($address1);
$user->addresses()->push($address2);
//do some stuff to maybe validate some things or pass the user object to some other controllers that handels it
//Save the user and the addresses here.
$user->save()
所以这与$ user-> addresses() - > saveMany([$ address1,$ address2])不同; 希望你们有人给我一个优雅的解决方案。
答案 0 :(得分:-1)
您应该将验证操作与保存操作区分开来。您可以通过在对象控制器中创建不同的方法来实现此目的。一个用于验证你将检查重复,类似等,然后如果验证通过,你应该重定向到你将创建和保存对象的商店路线。