我很难从我的选择框中保存所选信息。我收到了消息:
" SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键约束失败(
clientpad_notes
。notebooks
,CONSTRAINTnotebooks_contact_id_foreign
FOREIGN KEY(contact_id
)REFERENCEScontacts
(id
)ON DELETE CASCADE)(SQL:插入notebooks
(name
,contact_id
,{{ 1}},note_description
,note_body
,user_id
,updated_at
)值(assddsaasdadasasd,3,saS,ssss
,2 ,2018-03-03 19:18:51,2018-03-03 19:18:51)
我的问题是我正在从联系人表格中创建页面的信息。所以我将我的表与user_id的身份验证相关联,并且在notes表中我有contacts_id。我的目标是在创建新笔记时选择并保存通过其ID获取的联系人姓名。我有可能以错误的方式做这件事,我是Laravel的初学者,所以任何帮助都会受到赞赏。
这是我的笔记控制器并创建一个笔记页面。
NotesController.php
public function create() {
created_at
create.blade.php
$user_id = auth()->user()->id;
$user = User::find($user_id);
$contacts = Contact::find($user->contacts)->pluck('fullName');
return view('notebooks.create')->with('contacts', $contacts)->with('user', $user);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'name' => 'required',
'contact_id' => 'required',
'note_description' => 'required',
'note_body' => 'required',
]);
//Create a note
$notebook = new Notebook;
$notebook->name = $request->input('name');
$notebook->contact_id = $request->input('contact_id');
$notebook->note_description = $request->input('note_description');
$notebook->note_body = $request->input('note_body');
$notebook->user_id = auth()->user()->id; //currently logged in user show their notes
$notebook->save();
return redirect('/dashboard')->with('success', 'Your Note Was Created');
}
在保存笔记的数据库中,我有这个:
<div class="col-6 col-sm-3">
{{Form::label('contact_id', 'Choose your Contact')}}
{{Form::select('contact_id', $user->contacts->pluck('fullName'), $contacts, ['class' => 'form-control'])}}
</div>
</div>
答案 0 :(得分:0)
在与其他表格中使用Id时,你犯了一个小错误。您应该检查目标表中是否确实存在该ID。
$this->validate($request,[
'name' => 'required',
'contact_id' => 'required|exists:contacts', //This will validate that the contact ID actually exists.
'note_description' => 'required',
'note_body' => 'required',
]);
//Create a note
$notebook = new Notebook;
$notebook->name = $request->input('name');
$notebook->contact_id = $request->input('contact_id');
$notebook->note_description = $request->input('note_description');
$notebook->note_body = $request->input('note_body');
$notebook->user_id = auth()->user()->id; //currently logged in user show their notes
$notebook->save();
Laravel validation#rule-exists
$this->validate($request,[
'name' => 'required',
'contact_id' => 'required|exists:contacts,id', //check contacts table, for column ID
'note_description' => 'required',
'note_body' => 'required',
]);
答案 1 :(得分:0)
经过大量搜索后我找到了解决方案。在Create中我必须使用映射:
public function create(Contact $contact)
{
$user_id = Auth::user()->id;
$user = User::find($user_id);
$contacts = $user->contacts->mapWithKeys(function($contact){
return [$contact->id => $contact->fullName];
});
// dd ($contacts);
return view('notebooks.create')->with('contacts', $contacts)->with('user', $user);
}
然后在选择我刚刚呼吁:
{{Form::label('contact_id', 'Choose your Contact')}}
{{Form::select('contact_id', $contacts, null, ['class' => 'form-control'])}}
像魅力一样,以防其他人发现这个有用的