php - 如何使用 Laravel 在一条路线中获取两个 id?
问题描述
这是我的路线:
Route::get('/admin/job-seeker/search/employer/{employerId}', 'AdminJobSeekerSearchController@show')->name('admin.job-seeker.search.employer.show')->middleware('verified');
Route::get('/admin/job-seeker/search/employer/{employerId}/post-a-job/{jobPostId}', 'AdminEmployerJobPostsController@show')->name('admin.employer.post-a-job.show')->middleware('verified');
第一条路线的控制器:
public function show($employerId)
{
$user = Auth::user();
$jobSeekerProfile = JobSeekerProfile::all()->where('user_id', $user->id)->first();
$employerProfiles = EmployerProfile::limit(1)->where('id', $employerId)->get();
$jobPosts = JobPosts::all();
return view('admin.job-seeker.search.employer.show', compact('employerProfiles', 'id', 'jobSeekerProfile', 'jobPosts'));
}
第二条路线的控制器:
public function show($employerId, $jobPostId)
{
$user = Auth::user();
$jobSeekerProfile = JobSeekerProfile::all()->where('user_id', $user->id)->first();
$employerProfiles = EmployerProfile::limit(1)->where('id', $employerId)->get();
$jobPosts = JobPosts::all();
$jobPost = JobPosts::findOrFail($jobPostId);
return view('admin.employer.post-a-job.show', compact('jobSeekerProfile', 'employerProfiles', 'jobPosts', 'jobPost'));
}
我有两张桌子employer_profiles
和job_posts
. 雇主注册并创建个人资料后,他可以创建具有 2 个字段job_title
和job_description
. 例如,它可以是项目经理,然后是该角色的描述。
我收到此错误:
[Route: admin.employer.post-a-job.show] [URI: admin/job-seeker/search/employer/{employerId}/post-a-job/{jobPostId}] 缺少必需的参数。(查看:C:\xampp\htdocs\highrjobs\resources\views\includes\job_seeker_search_employers.blade.php)(查看:C:\xampp\htdocs\highrjobs\resources\views\includes\job_seeker_search_employers.blade.php)
job_seeker_search_employees.blade:
@if($employerProfiles)
@foreach($employerProfiles as $employerProfile)
<br><br>
<div class="col-md-6 offset-md-3">
<div class="card" style="width: 28rem;">
<img class="card-img-top" src="https://via.placeholder.com/400" alt="Card image cap" style="max-height:400px;">
<div class="card-body">
<h1 class="card-title text-uppercase" style="color: #5dad07;"><strong>{{ $employerProfile['immediate_contact'] }}</strong></h1>
<h3><strong>Positions Available:</strong><br>
@if($jobPosts->where('user_id', $employerProfile->user_id)->count() > 0)
@foreach($jobPosts->where('user_id', $employerProfile->user_id) as $jobPost)
<h5><a href="{{route('admin.employer.post-a-job.show', $employerProfile->user_id, $jobPost->id)}}" type="button">{{ $jobPost['job_title'] }}</a></h5>
@endforeach
@else
<h5>No Positions Available</h5>
@endif
</h3>
<h3><strong>Contact Details:</strong></h3>
<p>
<strong>Company Name:</strong> {{ $employerProfile['company_name'] }}<br>
<strong>Contact Email:</strong> {{ $employerProfile['email'] }}<br>
<strong>Phone:</strong> {{ $employerProfile['company_phone'] }}
</p>
<strong>Address:</strong>
<address>{{ $employerProfile['company_address'] }}</address>
</div>
</div>
</div>
<br><br><br>
@endforeach
@endif
解决方案
只需编辑您的 foreach 循环,您必须将数组作为选项传递以使用路由参数名称作为数组键进行路由:
@foreach($jobPosts->where('user_id', $employerProfile->user_id) as $jobPost)
<h5><a href="{{route('admin.employer.post-a-job.show', ['employerId' => $employerProfile->user_id, 'jobPostId' => $jobPost->id])}}" type="button">{{ $jobPost['job_title'] }}</a></h5>
@endforeach
推荐阅读
- c++ - 如何将 arm64 中 vaddv_u8 的结果视为霓虹灯寄存器
- python-asyncio - Python:通过完成订购任务后获取对原始任务的引用
- html - css 将父 div 扩展到子高度
- angular - 如何以角度4填充选定的下拉文本
- laravel - 多对多。在将附加列保存到数据库时出错
- php - PhpMailer 发送的电子邮件进入垃圾邮件,也在发送的安全详细信息中显示我的主机参数,如何解决这个问题?
- jquery - Jquery 验证器表单
- c++ - 二维数组中的用户输入 (C++)
- aws-lambda - 我需要创建一个 Lambda 函数,该函数根据用户说出的文本返回代码
- python - 有人可以解释 numpy 如何显示多维数组吗?