3

In normal situation we get input fields values like $request->input('input_name') but in my case my input names are dynamic and can be anything (comes from database).

I want to know how can I get data of such inputs?

Code

my form input is like:

//1
<input type="radio" value="{{$opt->title}}" name="{{$opt->group->title}}">
//2
<select name="{{$opt->group->title}}" class="form-control">
//3
<input class="options-checkbox" type="checkbox" value="{{$opt->title}}" name="{{$opt->group->title}}[]">

so each of this inputs has different names based on database, and i can't predict their names in controller in order to get their values.

Test DD

array:5 [▼
  "_token" => "QBY0WqF2WdqALxks22zjqpuBwplviHStBzTqzFzD"
  "COLORS" => "blue" //1
  "tester_group3ffhshg" => "title 33" //2
  "radio_group" => array:1 [▼
    0 => "hi" //3
  ]
  "quantity" => "1"
]

Is there anyway to get data from such inputs?

7
  • $request->all() gives you all of these data. Commented Feb 4, 2019 at 3:22
  • yes it gives me the dd i shared, but my question is how to get those specific inputs i mentioned? Commented Feb 4, 2019 at 3:23
  • If those input are from database then you can use database values again to get these from request Commented Feb 4, 2019 at 3:27
  • @SagarGautam I am using database values {{$opt->group->title}} the thing is this value is dynamic (can be anything) so i can't use $request->input('input_name') this input_name can be anything Commented Feb 4, 2019 at 3:30
  • So, fetch $opt->group->title value from database in the controller and then $request->only([$opt->group->title]) will gives you correct value you want. Commented Feb 4, 2019 at 3:45

1 Answer 1

2

I'm not going to answer in code (Meaning, I won't re-create the whole code) but I can give you the most optimal logic that you can use to solve this problem.

First of all, As your input fields are dynamic, You must have something in frontend controlling these inputs. At least, Javascript. If not, you'll require it.

Once you have Javascript in the frontend, On load and also whenever a user takes an action that might alter your fields, Update an array variable containing the names of those fields. For example, If you have fields named field_1 and field_2 on page load, add those field names to one array called request_fields.

After that, update it every time there is some change with fields. After some changes, let's say now you have three fields that you will require in your controller named field_23,field_34 and field_54 and you are updating it with every action.

Now, On form submit action, just append this array of the field that you require in your POST request of the form and you are good to go! Then, you can scan this array in the backend and get the request data as you desire!

A quick example on the controller might look something like this:

foreach($request->requiredFields as $field){
    echo $request->{$field};
}

This will solve your problem very easily as it's easy to implement and also it's easy to understand! Let me know if you have more questions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.