Missing route parameter for Route URI using Laravel?

Ryan Sacks :

I'm trying to create a Laravel Notification. I created a table called send_profiles. When a Candidate is logged in and searching through jobs, he can send his job profile to the employer. All of that data is in a table called job_seeker_profiles. I'm developing a Job Search type of application.

I created a new Notification class called SendProfile.php:

public function toDatabase($notifiable)
    {
        $user = Auth::user();

        return [
            'user_id' => Auth::user()->id,
            'employer_profile_id' => DB::table('send_profiles')->where('user_id', $user->id)->orderBy('id', 'desc')->offset(0)->limit(1)->get('employer_profile_id'),
        ];
    }

I don't know the best way to go about this but anyway this is my route. web.php:

Route::get('/admin/job-seeker/search/employer/{employerId}/post-a-job/{jobPostId}/send-profile', 'AdminEmployerJobPostsController@sendProfile')->name('admin.employer.post-a-job.show.send-profile')->middleware('verified');

AdminEmployerJobPostsController.php:

public function sendProfile($employerId, $jobPostId)
{

    $user = Auth::user();

    $jobSeekerProfile = JobSeekerProfile::all()->where('user_id', $user->id)->first();

    $employerProfile = EmployerProfile::limit(1)->where('id', $employerId)->get();

    $jobPosts = JobPosts::all();

    $jobPost = JobPosts::findOrFail($jobPostId);

    $user->sendProfile()->create();

    $employerProfile->notify(new SendProfile());

    return back()->with('send-profile', 'Your Profile has been sent!');

}

This is my error:

Missing required parameters for [Route: admin.employer.post-a-job.show.send-profile] [URI: admin/job-seeker/search/employer/{employerId}/post-a-job/{jobPostId}/send-profile]. (View: /Applications/XAMPP/xamppfiles/htdocs/highrjobs/resources/views/admin/employer/post-a-job/show.blade.php)

show.blade:

@extends('layouts.admin')

@section('pageTitle', 'Create a User')

@section('content')

    @include('includes.job_seeker_search_employers')

    <!-- The Modal -->
    <div class="modal" id="myModal5">
        <div class="modal-dialog">
            <div class="modal-content">

                <!-- Modal Header -->
                <div class="modal-header">
                    <h4 class="modal-title">{{ $jobPost->job_title }}</h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>

                <!-- Modal body -->
                <div class="modal-body">
                    <h5>{{ $jobPost->job_description }}</h5>
                </div>

                <!-- Modal footer -->
                <div class="modal-footer">

                    {!! Form::open(['method'=>'POST', 'action'=>'AdminEmployerJobPostsController@sendProfile', 'files'=>true, 'style'=>'width: 100%;']) !!}

                    <div class="form-group">
                        {!! Form::hidden('user_id', Auth::user()->id, ['class'=>'form-control']) !!}
                    </div>

                    <div class="form-group">
                        {!! Form::hidden('employer_profile_user_id', $employerProfile->id, ['class'=>'form-control']) !!}
                    </div>

                    <div class="row">
                        <div class="col">
                            {!! Form::button('Back', ['class'=>'btn btn-danger btn-block float-left', 'data-dismiss'=>'modal']) !!}
                        </div>
                        <div class="col">
                            {!! Form::submit('Send Profile', ['class'=>'btn btn-primary btn-block float-right']) !!}

                            {!! Form::close() !!}
                        </div>
                    </div>
                    <br><br><br><br>

                </div>

            </div>
        </div>
    </div>

@stop

If I remove the form, I at least don't get an error. So I actually think there is an issue with the form.

To be clear, all I want is to insert the user_id and the employer_profile_id into the send_profiles table and then send a notification to the employer.

miken32 :

Your route specifies a GET request to a URL that contains certain parameters:

/admin/job-seeker/search/employer/{employerId}/post-a-job/{jobPostId}/send-profile

Your form is using AdminEmployerJobPostsController@sendProfile as an action; this is translated into a URL by searching the route list, and choosing what Laravel thinks is most appropriate. Since you haven't passed anything to fill the employerId and jobPostId parameters, you're getting this error when the URL is generated.

Even if you could get the URL generated, you'd have a problem because your form is sending a POST request to this GET route.

What you need to do is ensure you have a POST route pointing to a new controller method. You won't pass any parameters to this route in the URL, so your controller method will only typically accept a Request object as a parameter. Second thing you should do is specify your form's target more accurately. Pass in the route name instead of making it guess.

public function sendProfile(Request $request)
{
    // you get this here so no need to pass it in the form
    $user = Auth::user();

    // your relations should be set up so you don't need to do this:
    // $jobSeekerProfile = JobSeekerProfile::all()->where('user_id', $user->id)->first();
    // instead do this:
    $jobSeekerProfile = $user->jobSeekerProfile();

    // a simple find is much neater than what you had
    $employerProfile = EmployerProfile::find($request->job_seeker_profile_user_id);

    // not sure why this is here?
    $jobPosts = JobPosts::all();

    // also your form isn't passing a job post ID
    $jobPost = JobPosts::findOrFail($request->jobPostId);

    // ??? creating an empty something?
    $user->sendProfile()->create();

    $employerProfile->notify(new SendProfile());

    return back()->with('send-profile', 'Your Profile has been sent!');

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=293705&siteId=1