传递给 Illuminate\Database\Eloquent\Builder::create() 的参数 1 必须是数组类型

Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array

我有一个模型,我打算将输入保存到两个不同的 table,但出现错误

Symfony\Component\Debug\Exception\FatalThrowableError Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, object given, called in C:\xampp\htdocs\blog\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php on line 23

第一个table是Transaction_in,第二个是Transaction_in_detail。我计划使用 idTransaction_in 作为 2 table 之间的连接器,并且该列不是主键(这是一个好习惯)?我可以将输入保存到 Transcation_in table 但仍然无法将输入保存到第二个 table 并且我不知道如何将 idTransaction_in 列作为连接器.

public function store(Request $request)
{
    $request->validate([
        'supplier_name' => 'required',
        'transaction_in_date' => 'required|before_or_equal:today',
        'device_type_name' => 'required',
        'device_brand_name' => 'required',
        'device_spec' => 'required|max:255',
        'price' => 'required',
        'amount' => 'required',
        'total_price' => 'required',
        'keterangan' => 'Nullable',

        ]);


    $transaction_in = new Transaction_in();
    $transaction_in->idTransaction_in = "0";
    $transaction_in->Supplier_id = $request->input('supplier_name');
    $transaction_in->tanggal_transaksi = $request->input('transaction_in_date');
    $transaction_in->save();
    $transaction_in->update(['idTransaction_in' => sprintf('TIN-%04d', $transaction_in->id)]);

    $lastid=Transaction_in::create($transaction_in)->idTransaction_in;
    if(count($request->device_type_name)>0){
        foreach ($request->device_type_name as $item => $v) {
            $data2=array(
                'Transaction_in_id' => $lastid,
                'DeviceType_id' => $request->device_type_name[$item],
                'DeviceBrand_id' => $request->device_brand_name[$item],
                'spek_device' => $request->device_spec[$item],
                'harga_device' => $request->price[$item],
                'jumlah_device' => $request->amount[$item],
                'total_harga_device' => $request->total_price[$item]
            );
            Transaction_in_detail::insert($data2);
        }
    }

    return redirect('/transactionsin')->with('success', 'Transaction success');
}

DeviceType_idDevicebrand_id是外键。

据我所知,您在这一行中遇到了问题

$lastid=Transaction_in::create($transaction_in)->idTransaction_in;

$transaction_in是对象,create方法需要传入array

如果其他一切正确,这应该有效:

$lastid=Transaction_in::create($transaction_in->toArray())->idTransaction_in;

您已经 save 数据,但您还使用 create。它将在您的数据库中创建重复记录。

我给你解决办法:

$transactionIn = Transaction_in::create([
    'idTransaction_id' => '0',
    'supplier_id' => $request->input('supplier_name') // why supplier name assigned supplier_id?
    'tanggal_transaksi' => $request->input('transaction_in_date'),
]);

$transactionIn = tap($transaction_in)->update([
    'idTransaction_in' => sprintf('TIN-%04d', $transactionIn->id)
]);

$lastId = $transactionIn->idTransaction_in;