Pooriya Mostaan
Pooriya Mostaan

Reputation: 405

How to get id from one page to another page in Laravel

I have a page to get some data from a user, this data will save in first_forms table, but I have other fields in the table for next page and they are null.

this is first_forms table :

first_forms table

In this table, project_name, customer_name, buyer, response, model will be saved in the first step in the first page choose.blade.php

This is store function in the controller:

FirstForm::create([
   'model' => $request['model'],
   'project_name' => $request['project_name'],
   'customer_name' => $request['customer_name'],
   'buyer' => $request['buyer'],
   'response' => $request['response'],
]);
return view('Form2.firstForm');

As you can see other fields in first_forms table will be null. After this user redirect to next page firstForm. Now in this page users have to complete other fields like: airflow, tcp, compress_number, etc. I want when user click on submit in this page, new fields in this page save in the previous row in first_forms table who complete in the previous page, how can I do this?

I think I have to get the id of who completes he previous page and update row with new fields in a new page, but I don't know how I can get the id and update row. Primary key is id.

This is first_forms table when user complete first page :

first_forms table

This is the data of who completed the first page. On the next page, those fields are null, have to be complete in this row.

Upvotes: 0

Views: 1155

Answers (1)

Akhtar Munir
Akhtar Munir

Reputation: 1769

You can simply check that

$first = FirstForm::create([
  'model' => $request['model'],
  'project_name' => $request['project_name'],
  'customer_name' => $request['customer_name'],
  'buyer' => $request['buyer'],
  'response' => $request['response'],
]);

//Below line will re-retrieve the record along with primary key which is `id`
$first = $first->fresh();

return view('Form2.firstForm',compact('first'));

In view you can access inserted data like $first->id Now place your id in a hidden field of the nextform and update your record according to that id. You can even show the previously inserted record in readonly fields of the nextform

In your nextform view

<input type="hidden" name="update_id" value="{{ $first->id ?? '' }}" />

When you submit the nextform with all other fields, you will get the hidden id as well. So again in your controller method you will do something like

FirstForm::where('id',request('update_id'))->update([
   //here goes your other fields of the nextform
]);

Upvotes: 1

Related Questions