方法 Illuminate\Database\Eloquent\Collection::create 不存在

Posted

技术标签:

【中文标题】方法 Illuminate\\Database\\Eloquent\\Collection::create 不存在【英文标题】:Method Illuminate\Database\Eloquent\Collection::create does not exist方法 Illuminate\Database\Eloquent\Collection::create 不存在 【发布时间】:2020-11-21 08:35:44 【问题描述】:

我想使用视图中的按钮将值从一个表(表格)复制到另一个表(发票)中的行。但出现错误:方法 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]);

这是我的视图,带有重复按钮:

 @extends('layouts.app')


@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Szczegóły abonamentu</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href=" route('proforms.index') "> Wstecz</a>
</div>
</div>
</div>


<div class="col-md-4">
<form action=" route('proforms.duplicate') " method="POST">
@csrf

<div class="input-group">
<input type="text" value="1" name="duplicate" class="form-control" readonly>
<span class="input-group-prepend">
<button type="submit" class="btn btn-primary">Wystaw fakturę</button>
</span>
</div>
</form>
</div>



<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Data wystawienia:</strong>
 $proform->proformdate 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Kontrahent:</strong>
 $proform->user_id 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Data sprzedaży:</strong>
 $proform->selldate 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Termin płatności:</strong>
 $proform->paymentdate 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Forma płatności:</strong>
 $proform->paymentmethod 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Miejsce wystawienia:</strong>
 $proform->city 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Status:</strong>
 $proform->status 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Uwagi:</strong>
 $proform->comments 
</div>
</div>
<div class="pull-left" style="margin: 15px;">
<h3>Pozycje proformy</h3>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Nazwa towaru lub usługi:</strong>
 $proform->name 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>PKWiU:</strong>
 $proform->PKWIU 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Ilość:</strong>
 $proform->quantity 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Jednostka:</strong>
 $proform->unit 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Cena netto jednostki:</strong>
 $proform->netunit 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Netto razem:</strong>
 $proform->nettotal 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Stawka VAT:</strong>
 $proform->VATrate 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Brutto jednostka:</strong>
 $proform->grossunit 
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Brutto razem:</strong>
 $proform->grosstotal 
</div>
</div>

</div>
@endsection

这是我的路线

<?php

use Illuminate\Support\Facades\Route;
Route::get('/', function()
return view('welcome');
);

Route::get('home');
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::group(['middleware' => ['auth']], function () 
Route::get('/search', 'UserController@search');
Route::get('/search2', 'ProductController@search2');
Route::get('/search3', 'ProformController@search3');
Route::get('/search4', 'InvoiceController@search4');

Route::post('/duplicate', 'ProformController@duplicate')->name('proforms.duplicate');
Route::get('data', 'UserController@index');
Route::get('posts', 'PostController@index');
Route::get('/prodview', 'TestController@prodfunct');

Route::resource('roles', 'RoleController');
Route::resource('users', 'UserController');
Route::resource('permissions', 'PermissionController');
Route::resource('products', 'ProductController');
Route::resource('invoices', 'InvoiceController');

Route::resource('category', 'CategoryController');
Route::resource('invoices', 'InvoiceController');
Route::resource('proforms', 'ProformController');
);

复制就是为了它。

【问题讨论】:

【参考方案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([..

【讨论】:

以上是关于方法 Illuminate\Database\Eloquent\Collection::create 不存在的主要内容,如果未能解决你的问题,请参考以下文章

静态方法与实例方法,类方法与对象方法

init()方法和构造方法的区别

GroovyGroovy 扩展方法 ( 扩展静态方法示例 | 扩展实例方法示例 | 扩展实例方法与扩展静态方法代码相同 )

Python中静态方法和类方法的区别

类方法和实例方法

Java中的构造方法与普通方法的区别? 啥情况下用构造方法啥情况下用普通的方法