gpt4 book ai didi

php - SQLSTATE[23000] : Integrity constraint violation: 1048 Column 'user_id' cannot be null error when assign forien key value?

转载 作者:行者123 更新时间:2023-11-29 07:29:25 24 4
gpt4 key购买 nike

我想分配外键值。外键是 p_members 表中的 user_id。原始键是 all_users 表中的 userId。但是当我尝试这样做时会出现以下错误。

我创建了两个单独的表来获取用户详细信息和获取 p-member 详细信息。

我是 Laravel 的初学者。

错误:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into `p_members` (`addNo`, `addStreet`, `addCity`, `intentToJoin`, `user_id`, `updated_at`, `created_at`) values (qwertfgyhj, ertfyguhj, erdftgyhuj, ertfgyhjk, , 2018-09-06 16:38:19, 2018-09-06 16:38:19))

我的 Controller 是:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\allUser;
use App\ORFOL;
use App\pMember;

class userController extends Controller
{
public function storeUsers(Request $request){
$table1=new allUser();


$table1->nameWithInitials = $request->input('name');
$table1->callingName = $request->input('callingName');
$table1->email = $request->input('email');
$table1->NIC = $request->input('nic');
$table1->contactNo = $request->input('contactNo');
$table1->password = bcrypt($request->input('password'));
$table1->type = $request->input('type');

$table1->save();


if($request->input('type') == 'P-Member'){
$table2=new pMember();

$table2->addNo = $request->input('addNo');
$table2->addStreet = $request->input('addStreet');
$table2->addCity = $request->input('addCity');
$table2->intentToJoin = $request->input('intentToJoin');

$table2->user_id = $table1->userId;

$table2->save();
return view('familyMember');

}

if(($request->input('type')) =='OR-FOL' ){
$table3=new ORFOL();

$table3->region = $request->input('region');
$table3->user_id = $table1->userId;

$table3->save();

}

if(($request->input('type')) =='' ){
$table3=new ORFOL();

$table3->region = $request->input('region');
$table2->user_id = $table1->userId;

$table3->save();

}


return redirect()->back();
}
}

最佳答案

放入你的模型:

protected $primaryKey = 'userId';

关于php - SQLSTATE[23000] : Integrity constraint violation: 1048 Column 'user_id' cannot be null error when assign forien key value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52209063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com