简体   繁体   中英

Store value that is stored in session laravel

The website is a reservation system, first the client must fill up the booking request form. After clicking next, the user will be moved to the booking summary where they can see the summary of the booking. I used session()->put to store the fields that was entered in the booking request form. This is working fine

$request->session()->put('firstName', $request->firstName);
$request->session()->put('lastName', $request->lastName);
$request->session()->put('phoneNumber', $request->phoneNumber);

My problem is that after the client click Confirm in the booking summary I can't store the values entered in the booking request form in the database. The values in the booking request form should be stored in the database only after the client clicked the Confirm button in the booking summary.

  $client_info = new client;
  $client_info->firstName = $request->session()->put('firstName', $request->firstName);
  $client_info->lastName = $request->session()->put('lastName', $request->lastName);
  $client_info->phoneNumber = $request->session()->put('phoneNumber', $request->phoneNumber);
  $client_info->emailAddress = $request->session()->put('emailAddress', $request->emailAddress);
  $client_info->save();

I tried using this, but I get the SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'firstName' cannot be null error. I think the session data is deleted? I'm new to laravel so I'm sorry if my explanation of the problem is confusing.

you should get value from session , while storing data into database :

  $client_info = new client();
  $client_info->firstName = $request->session()->get('firstName');
  $client_info->lastName = $request->session()->get('lastName');
  $client_info->phoneNumber = $request->session()->get('phoneNumber');
  $client_info->emailAddress = $request->session()->get('emailAddress');
  $client_info->save();

learn more about session : here

as you are getting this error :

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'firstName' cannot be null (SQL: insert into clients (firstName, lastName, phoneNumber, emailAddress, updated_at, created_at) values (, , , , 2018-11-30 10:24:56, 2018-11-30 10:24:56))

inside your client model :

protected  $fillable=['firstName','lastName','phoneNumber','emailAddress'];

and make sure in your database , client table : firstName,lastName,phoneNumber,emailAddress allow null values

also check that you are getting value from session , it may happen your session values are not properly stored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM