【问题标题】:Missing route parameter for Route URI using Laravel?使用 Laravel 的 Route URI 缺少路由参数?
【发布时间】:2020-06-25 20:14:33
【问题描述】:

我正在尝试创建 Laravel 通知。我创建了一个名为send_profiles 的表。当候选人登录并搜索工作时,他可以将他的工作资料发送给雇主。所有这些数据都在一个名为job_seeker_profiles 的表中。我正在开发求职类型的应用程序。

我创建了一个名为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'),
        ];
    }

我不知道最好的方法,但无论如何这是我的路线。 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!');

}

这是我的错误:

缺少 [Route: admin.employer.post-a-job.show.send-profile] [URI: admin/job-seeker/search/employer/{employerId}/post-a-job/{ jobPostId}/send-profile]。 (查看:/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

如果我删除表单,我至少不会收到错误消息。所以我实际上认为表单存在问题。

明确地说,我只想将user_idemployer_profile_id 插入send_profiles 表中,然后向雇主发送通知。

【问题讨论】:

  • 您的问题与通知无关,而是关于该主题:为什么在创建通知时将内容保存到数据库而不是将它们传递给通知?在基本代码中发送通知见this example,然后看看the notification class如何处理。
  • 你是说所有通知数据都应该只在通知表中?
  • 我是说您可以在创建通知时直接将内容传递给通知,而不是进行更多的数据库查找。您确实应该以尽量减少数据库访问为目标,因此如果您已经拥有信息,请使用它。
  • 是的,这就是为什么我只想传递一个user_id 和一个employer_profile_id,这样我就知道哪个应聘者向哪个雇主发送了表单请求。
  • 我尝试在通知上观看 YouTube 视频。我对这个话题感到很困惑。

标签: php mysql laravel routes laravelcollective


【解决方案1】:

您的路由指定对包含某些参数的 URL 的 GET 请求:

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

您的表单正在使用AdminEmployerJobPostsController@sendProfile 作为操作;通过搜索路由列表并选择 Laravel 认为最合适的,将其转换为 URL。由于您没有传递任何内容来填充 employerIdjobPostId 参数,因此在生成 URL 时会出现此错误。

即使您可以生成 URL,您也会遇到问题,因为您的表单正在向该 GET 路由发送 POST 请求。

您需要做的是确保您有一个指向新控制器方法的 POST 路由。您不会在 URL 中将任何参数传递给此路由,因此您的控制器方法通常只会接受 Request 对象作为参数。您应该做的第二件事是更准确地指定表单的目标。传入路由名称而不是让它猜测。

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

}

【讨论】:

  • 我决定把东西放在一个新的控制器中并使用 store 方法,但你是说通知数据应该只在 Notifications 表中吗?这就说得通了。也谢谢你,我看到了我上面犯的所有错误。我确实编辑了我原来的线程和代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-09
  • 2020-07-14
  • 1970-01-01
  • 2022-11-24
  • 2022-11-04
  • 2020-07-16
  • 2021-03-08
相关资源
最近更新 更多