【问题标题】:Laravel: Error 404 after clicking redirecting button in viewLaravel:单击视图中的重定向按钮后出现错误 404
【发布时间】: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-&gt;proform-&gt;findOrFail($request-&gt;duplicate);
  • 什么是$this-&gt;proform?如果我假设它是 Proform 模型,那么您的 findOrFail(1) 正在您的数据库中寻找 id 1,找不到它并返回 404

标签: php laravel


【解决方案1】:

findOrFailfirstOrFail 方法将检索查询的第一个结果;但是,如果没有找到结果,则会抛出 Illuminate\Database\Eloquent\ModelNotFoundException

$model = App\Flight::findOrFail(1);

$model = App\Flight::where('legs', '>', 100)->firstOrFail();

因此,如果数据库中不存在该 ID。 ModelNotFoundException 会给你一个 404 错误

如果未捕获到异常,则会自动将 404 HTTP 响应发送回用户。使用这些方法时,无需编写显式检查以返回 404 响应:

Route::get('/api/flights/{id}', function ($id) {
    return App\Flight::findOrFail($id);
});

【讨论】:

    猜你喜欢
    • 2021-10-30
    • 2019-08-09
    • 2017-08-14
    • 1970-01-01
    • 2013-08-09
    • 2014-12-31
    • 2015-09-22
    • 2020-01-08
    • 2011-02-19
    相关资源
    最近更新 更多