404 Not Found,但在Laravel 9中存在路由。

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

404 Not Found, but route exist in Laravel 9

问题

这是代码的翻译部分:

blade file code:

@foreach($lead as $lead)
  <tr>
    <td>
      <a href="{{ url('show_lead', $lead->id) }}">{{ $lead->first_name }} {{ $lead- >last_name }}</a>
    </td>
    <td>{{ $lead->company }}</td>
    <td>{{ $lead->email }}</td>
    <td>{{ $lead->phone }}</td>
    <td>{{ $lead->lead_source }}</td>
    <td>{{ $lead->lead_status }}</td>
    <td>
      <a class="btn btn-success fa fa-edit" href="{{ url('edit_lead', $lead->id) }}"></a>
      <a class="btn btn-danger fas fa-trash-alt" onclick="return confirm('Sure You Want To Delete This Lead')" href="{{ url('/delete_lead', $lead->id) }}"></a>    
    </td>
  </tr>
@endforeach

route:

Route::get('show_lead/{id}', [HomeController::class, 'show_lead']);

controller:

public function show_lead()
{
    $lead = lead::find($id);

    return view('admin.leads.show_lead', compact('lead'));
}
英文:

I am trying to hit on name and get the details of lead but every time it shows 404 not found

blade file code

@foreach($lead as $lead)
  <tr>
    <td>
      <a href="{{ url('show_lead', $lead->id) }}">{{ $lead->first_name }} {{ $lead- >last_name }}</a>
    </td>
    <td>{{ $lead->company }}</td>
    <td>{{ $lead->email }}</td>
    <td>{{ $lead->phone }}</td>
    <td>{{ $lead->lead_source }}</td>
    <td>{{ $lead->lead_status }}</td>
    <td>
      <a class="btn btn-success fa fa-edit" href="{{ url('edit_lead', $lead->id) }}"></a>
      <a class="btn btn-danger fas fa-trash-alt" onclick="return confirm('Sure You Want To Delete This Lead')" href="{{ url('/delete_lead', $lead->id) }}"></a>    
    </td>
  </tr>
@endforeach

route:

Route::get('show_lead/{id}', [HomeController::class, 'show_lead']);

controller

public function show_lead()
{
    $lead = lead::find($id);

    return view('admin.leads.show_lead', compact('lead'));
}

答案1

得分: 2

给你的路由命名:

Route::get('show_lead/{id}', [HomeController::class, 'show_lead'])->name('show_lead');

然后,在你的模板文件中,使用route($name, $parameters = [])助手函数调用这个路由:

<a href="{{ route('show_lead', ['id' => $lead->id]) }}"></a>

在你的控制器中,需要将你定义的{id}参数添加到控制器方法的参数中:

public function show_lead($id)
{
    $lead = Lead::find($id);

    return view('admin.leads.show_lead', compact('lead'));
}

最后,如果你因某种原因在开发中对路由进行了缓存,运行php artisan route:clear


使用路由模型绑定,你可以使用更少的代码实现相同的功能:

Route::get('show_lead/{lead}', [HomeController::class, 'show_lead'])->name('show_lead');
<a href="{{ route('show_lead', [$lead]) }}"></a>
public function show_lead(Lead $lead)
{
    return view('admin.leads.show_lead', compact('lead'));
}
英文:

You should give a name to your route.

Route::get(&#39;show_lead/{id}&#39;, [HomeController::class, &#39;show_lead&#39;])-&gt;name(&#39;show_lead&#39;);

Then, in your blade file, call this route using the route($name, $parameters = []) helper.

&lt;a href=&quot;{{ route(&#39;show_lead&#39;, [&#39;id&#39; =&gt; $lead-&gt;id]) }}&quot;&gt;&lt;/a&gt;

And in your controller, you need to add the {id} parameter you've defined to the controller method's arguments.

public function show_lead($id)
{
    $lead = lead::find($id);

    return view(&#39;admin.leads.show_lead&#39;, compact(&#39;lead&#39;));
}

Lastly, in case you've been caching your routes in development for some reason, run php artisan route:clear.


Using route model binding, you could do this with less lines

Route::get(&#39;show_lead/{lead}&#39;, [HomeController::class, &#39;show_lead&#39;])-&gt;name(&#39;show_lead&#39;);
&lt;a href=&quot;{{ route(&#39;show_lead&#39;, [$lead]) }}&quot;&gt;&lt;/a&gt;
public function show_lead(Lead $lead)
{
    return view(&#39;admin.leads.show_lead&#39;, compact(&#39;lead&#39;));
}

huangapple
  • 本文由 发表于 2023年3月1日 14:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600315.html
匿名

发表评论

匿名网友

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

确定