เมื่อผู้ใช้คลิกเพื่อสมัครงานตำแหน่งงานว่าง ระบบจะเปลี่ยนเส้นทางไปยังแบบฟอร์มที่มีรูปแบบ URL:
.../jobs/{job}/create`
โดยที่ {job} คือ id
ฉันต้องการส่ง id นี้ไปยังฟังก์ชัน store เพื่อให้ผู้ใช้สามารถกรอกแบบฟอร์ม ส่ง และข้อมูลทั้งหมดจะถูก เก็บไว้ในฐานข้อมูล รวมทั้ง id ที่ส่งผ่าน อย่างไรก็ตาม ดูเหมือนจะไม่ทำงาน และฉันได้รับข้อผิดพลาด array_merge() ต่อไปนี้:
array_merge(): พารามิเตอร์ที่คาดไว้ 2 จะเป็นอาร์เรย์ สตริงที่กำหนด
นี่คือเส้นทางที่ฉันใช้ เส้นทางหนึ่งเปลี่ยนเส้นทางไปยังหน้าแบบฟอร์มและอีกเส้นทางหนึ่งเพื่อจัดเก็บการส่งแบบฟอร์ม:
{-code- 2}
นี่คือฟังก์ชันในตัวควบคุมของฉัน
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>
นี่อาจเป็นข้อผิดพลาด
public function create($job)
{
return view(\'applications/create\', [\'job\' => $job]);
//OR
return view(\'applications/create\', compact(\'job\'));
}
และเพื่อส่งกลับ$job
ไปยังวิธีการจัดเก็บ คุณสามารถใช้พารามิเตอร์เส้นทางหรืออินพุตที่ซ่อนอยู่ ใช้พารามิเตอร์เส้นทาง
กำหนดเส้นทางได้
Route::post(\'/applications/{job}\', \'App\\Http\\Controllers\\ApplicationsController@store\')->name(\'applications\');
และคุณสามารถแก้ไขรหัสสำหรับแบบฟอร์ม
สมัครงาน
จากนั้นใน วิธีการจัดเก็บที่คุณสามารถเข้าถึงค่าสำหรับ$job
public function store(Request $request, $job)
{
// คุณยังสามารถรับค่าสำหรับ $job ผ่าน $request->route(\'job\');
$application = new \\App\\Models\\Aplicatio n;
// ตั้งค่าคุณสมบัติของวัตถุจากการป้อนข้อมูลของผู้ใช้
$application->job_id = $request->route(\'job\');
$application->user_id = $request->input( \'user_id\');
$application->message = $request->input(\'message\');
// ตั้งค่าป้ายกำกับเริ่มต้น (ใหม่)
$application->label_id = "1";
$application->save();
// การเปลี่ยนเส้นทางนี้ควรเป็นเส้นทางรับอื่น มิฉะนั้นจะทำให้ข้อผิดพลาดหายไป
// หรือส่งแบบฟอร์มต่อไป
ส่งคืนการเปลี่ยนเส้นทาง (\'แอปพลิเคชัน\ ');
}
อัปเดต
ทางเลือกง่ายๆ คือการป้อนข้อมูลที่ซ่อนอยู่ในแบบฟอร์ม ซึ่งจะส่งคืนค่าของ$job
สมัคร งาน
เส้นทางสามารถคงอยู่ได้โดยไม่มีพารามิเตอร์เส้นทาง
Route::post(\'/applications\', \'App\\Http\\Controllers\\ApplicationsController@store\')->name(\'applications\');
\ nและไม่มีอะไรเปลี่ยนแปลงในตัวควบคุมหากคุณใช้$request->job
เพื่อรับค่า
ฉันเดาว่าปัญหาอยู่ในบรรทัด
return view(\'applications/create\', $job);
return view(\'applications/create\', [\'job\'=>$job]);
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.