首页 > 解决方案 > 如何使用 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_profilesjob_posts. 雇主注册并创建个人资料后,他可以创建具有 2 个字段job_titlejob_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

标签: phpmysqllaravelroutes

解决方案


只需编辑您的 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

推荐阅读