方法 Illuminate\Database\Eloquent\Collection::create

我想使用视图中的按钮将一个表(形式)中的行的值复制到另一个表(发票)中的行。但出现错误:方法 Illuminate\Database\Eloquent\Collection::create 不存在。在线的:'grosstotal' => $proform->grosstotal,


这是我的控制器方法:


public function duplicate(Request $request)

{

 

$proform = $this->proform->findOrFail($request->duplicate);

$invoice = Invoice::all();


//something like this

$duplicated = $invoice->create([

'invoicenumber' => $proform->proformnumber,

'invoicedate' => $proform->proformdate,

'selldate' => $proform->selldate,

'user_id' => $proform->user_id,

'form_id' => $proform->form_id,

'currency_id' => $proform->currency_id,

'paymentmethod' => $proform->paymentmethod,

'paymentdate' => $proform->paymentdate,

'status' => $proform->status,

'comments' => $proform->comments,

'city' => $proform->city,

'autonumber' => $proform->autonumber,

'automonth' => $proform->automonth,

'autoyear' => $proform->autoyear,

'name' => $proform->name,

'PKWIU' => $proform->PKWIU,

'quantity' => $proform->quantity,

'unit' => $proform->unit,

'netunit' => $proform->netunit,

'nettotal' => $proform->nettotal,

'VATrate' => $proform->VATrate,

'grossunit' => $proform->grossunit,

'grosstotal' => $proform->grosstotal,


]);





return redirect()->route('invoices.show', ['invoice' => $duplicated]);

}


手掌心
浏览 107回答 1
1回答

当年话下

在你的代码中你有// all() gets you the collection of the Invoice Model instances of all the entries of invoice in the database.$invoice = Invoice::all();$duplicated = $invoice->create([..其中 $invoice 是发票模型实例的集合。您不能create对集合应用方法。您需要使用您拥有的模型进行创建Invoice。所以将上面的创建代码修改为$duplicated = Invoice::create([..
打开App,查看更多内容
随时随地看视频慕课网APP