【问题标题】:Integrity constraint violation: 1048 Column 'company_id' cannot be null laravel5.3违反完整性约束:1048 列“company_id”不能为空 laravel5.3
【发布时间】:2017-02-25 12:26:00
【问题描述】:

我正在尝试将 (Table company )(Company id -> primary key) 设置为 (Table branch)(company_id) 中的外键。

控制器:

public function savebranchinfo(Request $request){
        $validator = Validator::make($request->all(),[
            'name' => 'required|min:5',
            'email' =>'required|unique:branch',
            'address' =>'required',
            'contact' =>'required|min:11',
            'open_hours' =>'required',
        ]);
        if($validator->passes()){
            $branch = new Branch();
            $branch->company_id = $request->company_id;
            $branch->name = $request->name;
            $branch->email = $request->email;
            $branch->address = $request->address;
            $branch->contact = $request->contact;
            $branch->open_hours = $request->open_hours;
            if($branch->save()){
                $request->session()->flash('message','Successfully save!!');
                return redirect('/add/branch');
            }
        }else{
                return redirect('/add/branch')->withErrors($validator)->withInput();
        }
    }
}

迁移:

  public function up()
{
    Schema::create('branch', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('company_id')->unsigned();
        $table->foreign('company_id')->references('id')->on('company');
        $table->String('name');
        $table->String('email');
        $table->String('address');
        $table->String('contact');
        $table->String('open_hours');
        $table->timestamps();
    });
}

查看:

<input type="hidden" value="company_id{{$company->id}}">

错误:

SQLSTATE[23000]:违反完整性约束:1048 列“company_id”不能为空(SQL:插入branchcompany_idnameemailaddresscontact,@ 987654330@, updated_at, created_at) 值 (, Mahmood Son, mahmoodson@gmail.com, bilal gunj, 04237152734, 8am-8pm, 2017-02-25 12:06:35, 2017-02-25 12: 06:35))

【问题讨论】:

  • company_id 是自增列吗?然后将其从列表中删除。
  • 哦,很清楚它显示在分支表中,您正在尝试插入空白公司 ID,这就是问题所在。我的意思是('应该有一些价值',Mahmood Son,等等......

标签: php laravel-5


【解决方案1】:

在我看来,您忘记了表单中的输入名称:

<input type="hidden" name="company_id" value="{{$company->id}}">

【讨论】:

  • 请确认 $company->id 的值存在于 company 表中,只需查看您的 html 页面源代码即可。
猜你喜欢
  • 2020-03-29
  • 2018-03-04
  • 2018-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-20
  • 2021-08-16
  • 2019-10-06
相关资源
最近更新 更多