Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

limsocheat's avatar

How to keep original field name in Request Facade ($request) data when submit form in Laravel

I am working with existed SQL Server Database that most table field names have space. Example: Family Name

In my update view file. I have created field name with space as well (Family Name) to match with table field name.

When I try to update the model I got the problem with $request->all() method, because Request facade convert the input field name from Family Name to Family_Name. so I have to manually write the updating code in very fields that I got

$trainee    = Trainee::findOrFail($id);

$trainee->{'Family Name'}   = $request->input('Family_Name');
$trainee->{'Sex'}           = $request->input('Sex');
.......                     = ........

$trainee->save();

instead of

$trainee    = Trainee::findOrFail($id);

$input = $request->all();
$trainee->fill($input)->save();
IT IS A HUGE PAIN as I need to write many line of code repeatedly instead of using $input = $request->all();

Question:

How to prevent Request Facade from converting the input field name from Family Nameto Family_Name?

0 likes
7 replies
rin4ik's avatar

use except . it will pick up all data except family name

$request->except('family name');

for family name you can manually update

limsocheat's avatar

Thanks @rin4ik for you comment, it should consider as alternative way. but unfortunately most of my existed table field name are containing space.

As you can see this $request->all() result:

array:28 [▼
  "_method" => "PUT"
  "_token" => "yMHkk49RyHl6xXJeBalxz9naSr45ue9f0ysVXR8j"
  "Family_Name" => "Seorn123"
  "First_Name" => "Mory"
  "Date_Of_Birth" => "2000-01-01"
  "Sex" => "ស្រី/Female"
  "Family_nameKH" => null
  "First_nameKH" => null
  "Province" => "Kratie"
  "House_Number" => "17"
  "Phone_No" => "071 61 83 698"
  "Last_year_of_Schooling" => "9"
  "Maritial_Status" => "នៅលីវ/Single"
  "Have_Children" => "ទេ/No"
  "How_many_between_0_and_10" => "0"
  "Ethnic_Minority" => "ខ្មែរ/Khmer"
  "If_Yes_EMG" => "27"
  "ID_Poor_Card" => "ប័ណ្ណក្រីក្រកំរិត ២/ID Poor 2"
  "Guardian" => "Sao Pisey"
  "Contact_number_1" => "088-726-0477"
  "Village_chief" => "Sao Sanny"
  "Contact_number_2" => "09-746-1554"
  "Relationship" => null
  "Employment_Status" => "1"
  "Lack_of_skills_to_get_a_job" => "1"
  "Unemployment_Other" => null
  "Employment_Type" => "10"
  "What_was_your_job" => null
]

So, mean that I need to except of column name and rewrite it again the same way.

rin4ik's avatar

you should update them. make them with underscore or camel case.

1 like
limsocheat's avatar
limsocheat
OP
Best Answer
Level 4

I have found solution. I use php str_replace & array_combine function to convert from_ to space.

$data = array[
  "_method" => "PUT"
  "_token" => "yMHkk49RyHl6xXJeBalxz9naSr45ue9f0ysVXR8j"
  "Family_Name" => "Seorn123"
  "First_Name" => "Mory"
  "Date_Of_Birth" => "2000-01-01"
  "Sex" => "ស្រី/Female"
  "Family_nameKH" => null
  "First_nameKH" => null
  "Province" => "Kratie"
  "House_Number" => "17"
  "Phone_No" => "071 61 83 698"
  "Last_year_of_Schooling" => "9"
  "Maritial_Status" => "នៅលីវ/Single"
  "Have_Children" => "ទេ/No"
  "How_many_between_0_and_10" => "0"
  "Ethnic_Minority" => "ខ្មែរ/Khmer"
  "If_Yes_EMG" => "27"
  "ID_Poor_Card" => "ប័ណ្ណក្រីក្រកំរិត ២/ID Poor 2"
  "Guardian" => "Sao Pisey"
  "Contact_number_1" => "088-726-0477"
  "Village_chief" => "Sao Sanny"
  "Contact_number_2" => "09-746-1554"
  "Relationship" => null
  "Employment_Status" => "1"
  "Lack_of_skills_to_get_a_job" => "1"
  "Unemployment_Other" => null
  "Employment_Type" => "10"
  "What_was_your_job" => null
]

Then I convert them by using below function:

$keys        = str_replace( '_', ' ', array_keys( $data ) );
$results    = array_combine( $keys, array_values( $data ) );

Then we will get below result:

$result = array:28 [▼
  " method" => "PUT"
  " token" => "yMHkk49RyHl6xXJeBalxz9naSr45ue9f0ysVXR8j"
  "Family Name" => "Seorn123"
  "First Name" => "Mory"
  "Date Of Birth" => "2000-01-01"
  "Sex" => "ស្រី/Female"
  "Family nameKH" => null
  "First nameKH" => null
  "Province" => "Kratie"
  "House Number" => "17"
  "Phone No" => "071 61 83 698"
  "Last year of Schooling" => "9"
  "Maritial Status" => "នៅលីវ/Single"
  "Have Children" => "ទេ/No"
  "How many between 0 and 10" => "0"
  "Ethnic Minority" => "ខ្មែរ/Khmer"
  "If Yes EMG" => "27"
  "ID Poor Card" => "ប័ណ្ណក្រីក្រកំរិត ២/ID Poor 2"
  "Guardian" => "Sao Pisey"
  "Contact number 1" => "088-726-0477"
  "Village chief" => "Sao Sanny"
  "Contact number 2" => "09-746-1554"
  "Relationship" => null
  "Employment Status" => "1"
  "Lack of skills to get a job" => "1"
  "Unemployment Other" => null
  "Employment Type" => "10"
  "What was your job" => null
]

In the end I can use this fast way to update the data:

$trainee    = Trainee::findOrFail($id);

$trainee->fill($result)->save();

Hope it help someone.

1 like
rin4ik's avatar

but your token and method also without underscore . does it update?

limsocheat's avatar

Yes you are right, they were updated, I will find the way to fix them.

Please or to participate in this conversation.