目前,我有一张包含律师名单的表格,例如。我可以按路线/网络显示各州的律师名单:
// Lawyers per state
Route::get('/lawyers/{state}', function ($state) {
$states = DB::table('states')->get();
$lawyers = DB::table('lawyers')->join('states', 'states.abbr', '=', 'lawyers.state')
->where('states.abbr', '=', $state)
->orderBy('lawyers.featured', 'desc')
->get(['lawyers.*', 'states.name AS state_name']);
$statename = DB::table('states')->where('abbr', $state)->value('name');
return view('pages.lawyers', ['lawyers' => $lawyers, 'states' => $states, 'statename' => $statename]);
});
这正确显示了律师名单。
我想要的是为每位律师创建一个唯一的网址,例如带有网址结构/lawyers/{state}/{firstname}-{lastname}-{zipcode}
的个人资料页面视图,因此点击列表中的姓名会转到律师档案。
完成此任务的正确方法是:
lawyers.profile
视图谢谢!
答案 0 :(得分:1)
只需添加更多路线参数,就像您为{state}
所做的那样:
Route::get('/lawyers/{state}/{firstname}-{lastname}-{zipcode}', function ($state, $firstname, $lastname, $zipcode) {
// Logic
});
答案 1 :(得分:1)
在web.php中你可以做到。还在$ min内使用缓存执行一次db查询
$minutes = 10;
$lawyers = \Illuminate\Support\Facades\Cache::remember('', $minutes, function () {
return DB::table('lawyer')->get([
'firstname',
'lastname',
'zipcode',
'state',
]);
});
foreach ($lawyers as $lawyer) {
$url = sprintf('/%s/%s-%s-%s', $lawyer->state, $lawyer->firstname, $lawyer->lastname, $lawyer->zipcode);
Route::get($url, 'your action');
}