Laravel SQLSTATE[23000]: Integrity constraint viol

2019-07-15 08:24发布

I'm trying to make a basic simple CRUD using laravel framework, i manage to make a simple CRUD without no select form, but when i use select form, it's getting error message

FULL ERROR

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (activity_report_2015-04-22.tn_project, CONSTRAINT fk_client_id FOREIGN KEY (cv_client_id) REFERENCES 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) values (ada, 2016-02-18 23:52:45, TestProject, TestName, $client->cn_id, $invoice->cn_id, 1.2, JustTest))

The Value of cv_client_id and cv_invoice_method are from different table which is tn_client and tn_invoice_method, i noticed that why the value of cv_client_id and cv_invoice_method resulting query rathen than value, any idea?

if u guys need more information just let me know

View

<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>

Controller

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',
                )
        );
}

NB : if in my controller i put value of cv_client_id and cv_invoice_method manually, it's work fine

1条回答
萌系小妹纸
2楼-- · 2019-07-15 08:39

You have forgotten to echo $client->cn_id <option value='{{$client->cn_id}}'>{{$client->cv_name}}</option>;

Same here: <option value='{{$invoice->cn_id}}'>{{$invoice->cv_method}}</option>;

查看更多
登录 后发表回答