本文介绍了Laravel SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用laravel框架制作一个基本的简单CRUD,我设法制作了一个没有选择形式的简单CRUD,但是当我使用选择形式时,却收到了错误消息

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

完全错误

cv_client_id cv_invoice_method 的值来自不同的表,即 tn_client tn_invoice_method ,为什么 cv_client_id cv_invoice_method 的查询结果比值高,有什么想法?

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

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

NB:如果我在控制器中手动输入了 cv_client_id cv_invoice_method 的值,则效果很好

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

推荐答案

您忘记了回显$ client-> cn_id<option value='{{$client->cn_id}}'>{{$client->cv_name}}</option>;

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

与此处相同:<option value='{{$invoice->cn_id}}'>{{$invoice->cv_method}}</option>;

这篇关于Laravel SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:09