使用laravel雄辯來選擇特定的字段值。然而,我發現自己處理這個錯誤部門標識使用laravel雄辯從表中選擇數據
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '[{"id":1}]' for column 'department_id' at row 1 (SQL: insert into `opportunities` (`category`, `opportunity`, `vacancies`, `department_id`, `updated_at`, `created_at`) values (Attachement, Assistant, 2, [{"id":1}], 2017-05-04 02:12:04, 2017-05-04 02:12:04))
字段的值應該是int,它是從另一個表繪製如下
public function store(Request $request)
{
// return $request->all();
$this->validate($request,[
'department'=>'required',
'category'=>'required',
'description'=>'required|string',
'positions'=>'required|integer',
]);
//dd($request); table('departments')->where('dep_name', '$dep')
$dep = $request->department;
$department=Department::all();
//dd($department);
$department_id = Department::select('id')->where('dep_name','=', $dep)->get();
// return $department_id;
$newOpportunity = Opportunity::create(
[
'category'=>$request->category,
'opportunity'=>$request->description,
'vacancies'=>$request->positions,
'department_id'=>$department_id,
]
);
$newOpportunity ->save();
return redirect('admin/');
}
改變這一行'department_id'=> $ department_id [0] - > id, – JYoThI