Laravel 项目未定义路由

huangapple go评论54阅读模式
英文:

Laravel Project undefined route

问题

我试图访问 Blade 模板中的路由,所以我遇到了以下错误:

Route [$name] 未定义。

Route::patch('/update_merchant/{merchant_id}', 'MerchantsController@update')->name('update');

Blade 模板

<form name="merchants_data" id="merchants_data" method="post" action="{{route('update_merchant.update', [$item->merchant_id])}}">
    {{ csrf_field() }}
    {!! method_field('patch') !!}

控制器

public function update(Request $request, $merchant_id)
{
    // 验证所需字段(使用一些正则表达式验证数值)
    $request->validate([
        'fees'=>'required',
    ]); 
    $merchants = Merchants_data::findorfail($merchant_id);
    // 从 Blade 模板表单中获取值
    $merchants->fees = $request->get('fees');
    $merchants->save();

    return redirect('/manage_merchants')->with('success', '交易费用已更新。'); 
}
英文:

I am tring to access the route on blade template, so I getting the error below:

Route \[{$name}\] not defined.
Route::patch(&#39;/update_merchant/{merchant_id}&#39;, &#39;MerchantsController@update&#39;)-&gt;name(&#39;update&#39;);

Blade template

&lt;form name=&quot;merchants_data&quot; id=&quot;merchants_data&quot; method=&quot;post&quot; action=&quot;{{route(&#39;update_merchant.update&#39;, [$item-&gt;merchant_id])}}&quot;&gt;      
    {{ csrf_field() }}    
    {!! method_field(&#39;patch&#39;) !!} 

Controller

public function update(Request $request, $merchant_id)
{
    // Validation for required fields (and using some regex to validate our numeric value)
    $request-&gt;validate([
        &#39;fees&#39;=&gt;&#39;required&#39;,
    ]); 
    $merchants = Merchants_data::findorfail($merchant_id);
    // Getting values from the blade template form
    $merchants-&gt;fees = $request-&gt;get(&#39;fees&#39;);
    $merchants-&gt;save();

    return redirect(&#39;/manage_merchants&#39;)-&gt;with(&#39;success&#39;, &#39;Transaction Fee updated.&#39;); 
}

答案1

得分: 3

你的路由名称似乎是"update",但你调用的是"update_merchant.update"。

英文:

it seems your route name is "update" but you call "update_merchant.update"

答案2

得分: 3

根据 Mehran 的说法,你将路由命名为 'update',并在表单中调用 'update_merchant.update'。当你使用

...->name('foo');

这就是你必须在

{{ route('foo') }}

中使用的名称。你不需要在 'route' 属性中放入 URL,只需使用名称。

英文:

As Mehran said, you named your route 'update' and you called 'update_merchant.update' in your form. When you use

...-&gt;name(&#39;foo&#39;);

That's the name you have to use in the

{{ route(&#39;foo&#39;) }}

You don't put URL in the 'route' asset, just the name.

答案3

得分: 2

看起来您已经将路由的名称更改为update...

您可以执行以下操作:转到项目的根目录,然后在命令行中运行

php artisan route:list

这将为您提供当前应用程序中所有路由的列表...

英文:

It looks like you have changed the name of the route to update...

you can do one thing is go to the root of the project in your command line.. and look up

php artisan route:list

This should give you a list of all the routes you currently have in your application ...

huangapple
  • 本文由 发表于 2023年3月9日 21:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定