method\database\eloquent\collection::create不存在

kg7wmglp  于 2021-07-27  发布在  Java
关注(0)|答案(1)|浏览(423)

我想使用视图中的按钮将值从一个表(proforms)的行复制到另一个表(invoices)的行。但获取错误:方法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');
});

复制是为了它。

m4pnthwp

m4pnthwp1#

在你的代码里

// 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([..

相关问题