我正在尝试使用laravel框架制作基本的简单CRUD,我设法制作了没有选择形式的简单CRUD,但是当我使用选择形式时,却收到错误消息
完全错误
SQLSTATE [23000]:完整性约束违规:1452不能添加或更新子行,外键约束失败(activity_report_2015-04-22。tn_project,约束fk_client_id外键(cv_client_id)参考tn_client(cn_id))(SQL:INSERT INTO tn_project(cv_created_by,cn_created_at,cv_id,cv_name,cv_client_id,cn_invoice_method,cn_project_rate,cn_note)值(ada,2016-02-18 23:52:45,TestProject,TestName,$ client-> cn_id,$ invoice-> cn_id,1.2,JustTest))
activity_report_2015-04-22
tn_project
fk_client_id
cv_client_id
tn_client
cn_id
cv_created_by
cn_created_at
cv_id
cv_name
cn_invoice_method
cn_project_rate
cn_note
的价值 cv_client_id 和 cv_invoice_method 来自不同的表,该表是 tn_client 和 tn_invoice_method ,我注意到,为什么价值 cv_client_id 和 cv_invoice_method 结果查询比Rathen市的价值,任何想法?
如果你们需要更多信息,请告诉我
看法
<div class="form-group"> <label class="col-md-3 control-label">Client</label> <div class="col-md-9"> <select name="clientId" id="clientId" class="form-control" placeholder="Select Client"> @foreach ($clients as $client) <option value='$client->cn_id'>{{$client->cv_name}}</option>; @endforeach </select> </div> </div> <div class="form-group"> <label class="col-md-3 control-label">Invoice Method</label> <div class="col-md-9"> <select type="hidden" name="invoiceId" id="invoiceId" class="form-control"> @foreach ($invoices as $invoice) <option value='$invoice->cn_id'>{{$invoice->cv_method}}</option>; @endforeach </select> </div> </div>
控制器
public function createOrEdit(){ $currentUsername = Auth::user()->name; $isUpdate = false; $projectId = Input::get('prevId'); //populate data $project = new Project; if($projectId != ""){ $project = Project::where('cv_id','=',$projectId)->firstOrFail(); $project->cv_updated_by = $currentUsername; $project->cn_updated_at = Carbon::now(); $isUpdate = true; } else{ $project->cv_created_by = $currentUsername; $project->cn_created_at = Carbon::now(); } $project->cv_id = Input::get('projectId'); $project->cv_name = Input::get('projectName'); $project->cv_client_id = Input::get('clientId'); $project->cn_invoice_method = Input::get('invoiceId'); $project->cn_project_rate = Input::get('projectRate'); $project->cn_note = Input::get('note'); //execute if($isUpdate){ Log::info("entering update mode"); Project::where('cv_id','=',$projectId)->update(['cv_id'=>$project->cv_id, 'cv_name'=>$project->cv_name, 'cv_client_id'=>$project->cv_client_id, 'cn_invoice_method'=>$project->cn_invoice_method, 'cn_project_rate'=>$project->cn_project_rate, 'cn_note'=>$project->cn_note, 'cn_updated_at'=>$project->cn_updated_at, 'cv_updated_by'=>$project->cv_updated_by]); }else{ $project->save(); } return Response::json( array( 'content' => Project::with('client','invoice')->get(), 'status' => 'success', ) ); }
注意:如果我在控制器中手动输入了 cv_client_id 和 cv_invoice_method的 值,则可以正常工作
您忘记了回显$ client-> cn_id <option value='{{$client->cn_id}}'>{{$client->cv_name}}</option>;
<option value='{{$client->cn_id}}'>{{$client->cv_name}}</option>;
同样在这里: <option value='{{$invoice->cn_id}}'>{{$invoice->cv_method}}</option>;
<option value='{{$invoice->cn_id}}'>{{$invoice->cv_method}}</option>;