Using different html input name than database column name in Laravel
P粉818125805
P粉818125805 2024-01-16 12:40:25
0
2
301

How to use a different html input name than the column name I want to save this input data to

I usually use this method because all html input names are equal to the database columns:

Model::create($request->all());

Now I know I can use this:

Model::create([
'name' => $request->name,
'feild' = > $request=>value,
etc.
]);

But I have a lot of values ​​and I don't want to rewrite it over and over again, so is there a way to combine $request->all() with the second method?

P粉818125805
P粉818125805

reply all(2)
P粉627027031

I found that the PHP array_merge() function can be used as follows:

Model::create(array_merge($request->all(),['DB fieldName' => $newValue]));
P粉731977554

One way is to add the fields using https://laravel.com /docs/9.x/collections#method-merge merge() and use https://laravel. com/docs/9.x/collections#method-only Return only fields to be added to the model, or https://laravel.com/docs/9.x/collections#method-except except() inverse

$request->merge([
  'new_column_name' => $the_value_you_want_to_use,
  'old_column_name' => the_value_you_donot_want
]);
$request->only('new_column_name')

// or

$request->except('old_column_name')
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!