Ryan Sacks
Ryan Sacks

Reputation: 522

How to get two ids in one route using Laravel?

Here are my routes:

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');

Controller for first Route:

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'));
    }

Controller for second route:

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'));

    }

I have two tables employer_profiles and job_posts. After an Employer Registers and creates a profile, he can create job posts with 2 fields job_title and job_description. For example it could be Project Manager and then a description for this role.

I'm getting this error:

Missing required parameters for [Route: admin.employer.post-a-job.show] [URI: admin/job-seeker/search/employer/{employerId}/post-a-job/{jobPostId}]. (View: C:\xampp\htdocs\highrjobs\resources\views\includes\job_seeker_search_employers.blade.php) (View: 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

Upvotes: 2

Views: 625

Answers (1)

Ruben Danielyan
Ruben Danielyan

Reputation: 768

Just edit your foreach loop, you have to pass an array as options to rout using route param name as array key:

@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

Upvotes: 3

Related Questions