当用户点击申请某个职位空缺时,他们会被重定向到一个 URL 格式的表单:
.../jobs/{job}/create`
其中 {job} 是 ID。
我想把这个id传递给store函数,这样用户就可以填写表单,提交了,所有的数据都会存储在数据库中,包括传递过来的id。但是,它似乎不起作用,我收到以下 array_merge() 错误:
<块引用>array_merge(): 期望参数 2 是一个数组,给定的字符串
这是我使用的路由,一个重定向到表单页面,另一个用于存储表单提交:
Route::get('/jobs/{job}/create', 'App\Http\Controllers\ApplicationsController@create');
Route::post('/applications', 'App\Http\Controllers\ApplicationsController@store');
这些是我的控制器中的功能
public function create($job)
{
return view('applications/create', $job);
}
public function store(Request $request, $job)
{
$application = new \App\Models\Application;
// Set object properties from the user input
$application->job_id = $request->input($job);
$application->user_id = $request->input('user_id');
$application->message = $request->input('message');
// Set default label (new)
$application->label_id = "1";
$application->save();
return redirect('applications');
}
这是我的表格
<div class="wrapper bg-light">
<h1>Apply for job</h1>
<form action="/applications" method="post">
{{ csrf_field() }}
<label for="message">Write a motivation letter</label>
<br>
<textarea name="message" id="message" cols="30" rows="10"></textarea>
<br><br>
<label for="user_id">User id</label>
<input type="number" name="user_id" id="user_id">
<br><br>
<input type="submit" value="Submit">
</form>
</div>
答案 0 :(得分:0)
我想问题是在线的
return view('applications/create', $job);
改成
return view('applications/create', ['job'=>$job]);
答案 1 :(得分:0)
这可能是错误的
public function create($job)
{
return view('applications/create', ['job' => $job]);
//OR
return view('applications/create', compact('job'));
}
并且要将 $job
传回 store 方法,您可以使用路由参数或隐藏输入。以路由参数方式
路由定义可以
Route::post('/applications/{job}', 'App\Http\Controllers\ApplicationsController@store')->name('applications');
你可以修改表单的代码
<div class="wrapper bg-light">
<h1>Apply for job</h1>
<form action="{{ route('applications', ['job' => $job]) }}" method="post">
{{ csrf_field() }}
<label for="message">Write a motivation letter</label>
<br>
<textarea name="message" id="message" cols="30" rows="10"></textarea>
<br><br>
<label for="user_id">User id</label>
<input type="number" name="user_id" id="user_id">
<br><br>
<input type="submit" value="Submit">
</form>
</div>
然后在 store 方法中您可以访问 $job
的值
public function store(Request $request, $job)
{
//You can also get the value for $job via $request->route('job');
$application = new \App\Models\Application;
// Set object properties from the user input
$application->job_id = $request->route('job');
$application->user_id = $request->input('user_id');
$application->message = $request->input('message');
// Set default label (new)
$application->label_id = "1";
$application->save();
//This redirect should be some other get route else it will keep error out
//or keep submitting the form
return redirect('applications');
}
更新
一个简单的替代方法是在表单中有一个隐藏的输入,它将传回 $job
的值
<div class="wrapper bg-light">
<h1>Apply for job</h1>
<form action="/applications" method="post">
{{ csrf_field() }}
<input name="job" value="{{ $job }}" type="hidden" />
<label for="message">Write a motivation letter</label>
<br>
<textarea name="message" id="message" cols="30" rows="10"></textarea>
<br><br>
<label for="user_id">User id</label>
<input type="number" name="user_id" id="user_id">
<br><br>
<input type="submit" value="Submit">
</form>
</div>
路由可以不带路由参数
Route::post('/applications', 'App\Http\Controllers\ApplicationsController@store')->name('applications');
如果您使用 $request->job
获取值,则在控制器中没有任何变化