英文:
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('show_lead/{id}', [HomeController::class, 'show_lead'])->name('show_lead');
Then, in your blade file, call this route using the route($name, $parameters = [])
helper.
<a href="{{ route('show_lead', ['id' => $lead->id]) }}"></a>
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('admin.leads.show_lead', compact('lead'));
}
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('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'));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论