阅读(2563) (0)

Laravel 8 复制模型

2021-07-07 11:41:10 更新

您可以使用 replicate 方法复制一个新的未保存到数据库的实例, 当模型实例共享许多相同的属性时,这个方法非常好用。

$shipping = App\Models\Address::create([
    'type' => 'shipping',
    'line_1' => '123 Example Street',
    'city' => 'Victorville',
    'state' => 'CA',
    'postcode' => '90001',
]);

$billing = $shipping->replicate()->fill([
    'type' => 'billing'
]);

$billing->save();