使用 Laravel 的路由 URI 缺少路由参数?
Missing route parameter for Route URI using Laravel?
我正在尝试创建 Laravel 通知。我创建了一个名为 send_profiles
的 table。当候选人登录并搜索工作时,他可以将他的工作简介发送给雇主。所有这些数据都在一个名为 job_seeker_profiles
的 table 中。我正在开发求职类型的应用程序。
我创建了一个名为 SendProfile.php
:
的新通知 class
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!');
}
这是我的错误:
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">×</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_id
和 employer_profile_id
插入 send_profiles
table,然后向雇主发送通知。
您的路由指定了对 URL 的 GET 请求,其中包含特定参数:
/admin/job-seeker/search/employer/{employerId}/post-a-job/{jobPostId}/send-profile
您的表单正在使用 AdminEmployerJobPostsController@sendProfile
作为一项操作;通过搜索路由列表,并选择 Laravel 认为最合适的内容,将其转换为 URL。由于您没有传递任何内容来填充 employerId
和 jobPostId
参数,因此在生成 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!');
}
我正在尝试创建 Laravel 通知。我创建了一个名为 send_profiles
的 table。当候选人登录并搜索工作时,他可以将他的工作简介发送给雇主。所有这些数据都在一个名为 job_seeker_profiles
的 table 中。我正在开发求职类型的应用程序。
我创建了一个名为 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!');
}
这是我的错误:
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">×</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_id
和 employer_profile_id
插入 send_profiles
table,然后向雇主发送通知。
您的路由指定了对 URL 的 GET 请求,其中包含特定参数:
/admin/job-seeker/search/employer/{employerId}/post-a-job/{jobPostId}/send-profile
您的表单正在使用 AdminEmployerJobPostsController@sendProfile
作为一项操作;通过搜索路由列表,并选择 Laravel 认为最合适的内容,将其转换为 URL。由于您没有传递任何内容来填充 employerId
和 jobPostId
参数,因此在生成 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!');
}