3

I have this code in a larger function that pulls data from an HTTP Post (using Laravel) and puts it into an array for further processing. I feel like there has to be a better way to accomplish putting these fields into an array, but I'm not sure how. Suggestions?

    $fields = array(
        'env' => $request->get('env'),
        'subtype' => $request->get('subtype'),
        'submitter' => $request->get('submitter'),
        'problemSummary' => $request->get('problemSummary'),
        'problemDescription' => $request->get('problemDescription'),
        'resolutionCode' => $request->get('resolutionCode'),
        'resolutionDetails' => $request->get('resolutionDetails'),
        'status' => $request->get('status'),
        'account' => $request->get('account'),
        'phone' => $request->get('phone'),
        'residenceHall' => $request->get('residenceHall'),
        'roomNumber' => $request->get('roomNumber'),
        'buildingName' => $request->get('buildingName'),
        'buildingNumber' => $request->get('buildingNumber'),
        'source' => $request->get('source'),
        'submittedVia' => $request->get('submittedVia'),
        'internalNotes' => $request->get('internalNotes'),
        'computerType' => $request->get('computerType'),
        'computerVendor' => $request->get('computerVendor'),
        'operatingSystem' => $request->get('operatingSystem'),
        'recentBackup' => $request->get('recentBackup'),
        'antiVirus' => $request->get('antiVirus'),
        'acceptAgreement' => $request->get('acceptAgreement'),
    );
5
  • Define "better way". This looks perfectly fine. Commented Jul 18, 2016 at 23:20
  • Doesn't it have an ->all() method or something like that? Commented Jul 18, 2016 at 23:21
  • @FirstOne Yes, there is a $request->all() method but in case of errors from the posting side (extraneous fields, for example) I need to make sure I get only these fields. ->all() will send me everything. Commented Jul 18, 2016 at 23:23
  • @SverriM.Olsen This works, but at least in my opinion, seems messy. I was considering a loop like zerkms suggested. Commented Jul 18, 2016 at 23:24
  • 1
    @Ross What is the problem? The code may seem quite "bulky", if you like, but it tells you exactly what is happening. A loop would only complicate it. It is always better for code to be straight-forward than for it to do "clever" things because it looks better. Commented Jul 18, 2016 at 23:30

2 Answers 2

4

What's wrong with:

$fields = $request->only('env', 'subtype', 'submitter', ... );

or:

$fieldNames = ['env', 'subtype', 'submitter', ...];
$fields = $request->only($fieldNames);
Sign up to request clarification or add additional context in comments.

2 Comments

This looks better than my approach. +1
I had no idea this was a thing! This looks perfect. Thanks!
2

You could do something like this:

// Define the field names you want to get from the request
/*
$fields = array(
    'env',
    'subtype',
    'submitter',
    ...
);
*/

$data = array();
foreach($fields as $v){
    $data[$v] = $request->get($v);
}

It will only get the values from the keys of the request defined in $fields and it will put in the same key in the $data array.

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.