如何从同一商店中的创建继承数据?

How to inherit data from a create within the same store?

我在同一个 function store 中有 2 个 create,但是第一个 create 的一个字段必须与第二个创建的字段相同。

可能吗?

DataController.php

public function store(Request $request)
    {
        SendData::create($request->validated());

        // How I take here data from firt ::create ??
        $random_data_from_form1 = ??

        $data_from_form2 = $request->input('data_form2');

        SendMoreData::create([
           'field1' => $random_data_from_form1,
           'field2' => $data_from_form2
        ]);
    }

create() 方法 return SendData 的新模型实例。

您可以像这样更新您的代码:

public function store(Request $request)
    {
        $new_object = SendData::create($request->validated());

        // How I take here data from firt ::create ??
        $random_data_from_form1 = $new_object->your_field;

        $data_from_form2 = $request->input('data_form2');

        SendMoreData::create([
           'field1' => $random_data_from_form1,
           'field2' => $data_from_form2
        ]);
    }