我想通过一次注册将数据存储在3个表中 表格但出现错误。我已经尝试过了,但不是 由于违反主键约束而允许
控制器
class UserController extends Controller
{
public function registerLawyer(Request $request){
$firstname = $request->get('firstname');
$lastname = $request->get('lastname');
$country = $request->get('country');
$role = $request->get('role');
$dob = $request->get('dob');
$email = $request->get('email');
$pass = $request->get('password');
$aop = $request->get('pracitces');
$licensenum = $request->get('licenseNumber');
$license = $request->get('license');
$data = new User();
$data->firstname = $firstname,
$data->lastname = $lastname,
$data->country = $country,
$data->role = $role,
$data->dob = $dob,
$data->email = $email,
$data->password = $pass,
$data->save();
DB::table('lawyer')->insert([
'practices' => $aop
]);
DB::table('license')->insert([
'licenseno' => $licensenum,
'institution' => $license
]);
return view('registerlawyer');
}
}
registerlawyer.blade.php
<form method="post">
<h1>Signup-Legal Professional</h1>
<input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">
Enter Firstname :<input type="text" name="firstname" required><br>
Enter Lastname :<input type="text" name="lastname" required><br>
Country <select name="country" required="required" class="form-control">
<option value="select" selected="selected">Select Country</option>
<option value="Cameroon">Cameroon</option>
<option value="Canada">Canada</option>
<option value="Cape Verde">Cape Verde</option>
<option value="Cayman Islands">Cayman Islands</option>
<option value="Central African Republic">Central African Republic</option>
<option value="Chad">Chad</option>
<option value="Chile">Chile</option>
</select><br>
Enter Company :<input type="text" name="company" required><br>
Enter role :<input type="text" name="role" required><br>
Enter DOB :<input type="date" name="dob"><br>
Enter emailaddress :<input type="text" name="email" required><br>
Enter password :<input type="password" name="password" required><br>
Select Area Of practices <select name="pracitces">
<option>Individuals</option>
<option>Aerospace</option>
<option>Agriculture</option>
<option>Blockchain</option>
<option>Construction</option>
<option>Education</option>
<option>Electronics</option>
</select><br>
Enter License Number :<input type="number" name="licenseNumber" required><br>
Enter License Institution<input type="text" name="license" required><br>
<input type="submit" value="Submit">
</form>
我得到的错误是
SQLSTATE[HY000]: General error: 1364 Field 'u_id' doesn't have a default value (SQL: insert into `lawyer` (`practices`) values
(构造))
即使没有注册用户,我怎么获得u_id?
答案 0 :(得分:2)
$user_data = DB::table('users')->insertGetId([
'firstname' => $firstname,
'lastname' => $lastname,
'country' => $country,
'role' => $role,
'dob' => $dob,
'email' => $email,
'password' => $pass,
]);
$lawyer_data = DB::table('lawyer')->insertGetId([
'u_id' => $user_data,
'practices' => $aop,
]);
$license_data = DB::table('license')->insertGetId([
'lawyer_id' => $lawyer_data,
'licenseno' => $licensenum,
'institution' => $license,
]);
答案 1 :(得分:1)
使用此代码:
$user_data = DB::table('users')->insert([
'firstname' => $firstname,
'lastname' => $lastname,
'country' => $country,
'role' => $role,
'dob' => $dob,
'email' => $email,
'password' => $pass,
]);
$user = DB::table('users')->where(['firstname'=>$firstname])->first();
$lawyer_data = DB::table('lawyer')->insert([
'u_id' => $user->id,
'practices' => $aop
]);
$law = DB::table('lawyer')->where(['u_id'=>$user->id])->first(['id']);
$license_data = DB::table('license')->insert([
'lawyer_id' => $law->u_id,
'licenseno' => $licensenum,
'institution' => $license
]);
return redirect('welcome');