【发布时间】:2020-08-10 20:39:11
【问题描述】:
当我尝试使用视图中的按钮将重复记录从一个 wiev 和表复制到另一个视图和表时,Laravel 显示错误 404。此按钮重定向到路由并将路由重定向到控制器,但我不知道在哪里漏洞。我不知道该怎么办。有人帮帮我吗?
这是带有重定向按钮的视图的一部分。
<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>
这是它的路线:
Route::post('/duplicate', 'ProformController@duplicate')->name('proforms.duplicate');
这是它的控制器方法:
public function duplicate(Request $request)
{
$autoyear = date('Y');
$automonth = date('m');
$autonumber = DB::table('proforms')
->select(DB::raw('MAX(autonumber) as autonumber'))
->where('automonth', '=', '$automonth')
->where('autoyear', '=', '$autoyear')
->get();
@$auto = @$autonumber[0]->autonumber;
@$auto[0]++;
$user = User::all('showname','id');
$proform = $this->proform->findOrFail($request->duplicate);
$proforms = Proform::sortable()->paginate(5);
/*
//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,
]); */
DB::table('invoices')->insert([
['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,
'autonumber' => $number,
'automonth' => $automonth,
'autoyear'=> $autoyear],
]);
return view('proforms.show',compact('proform', 'user'))
->with('sukces','Faktura wystawiona');
}
【问题讨论】:
-
你的错误在这一行
$proform = $this->proform->findOrFail($request->duplicate); -
什么是
$this->proform?如果我假设它是 Proform 模型,那么您的findOrFail(1)正在您的数据库中寻找 id 1,找不到它并返回 404