2

I have a form request which I need to validate . If I dd the $request->all() it shows me the following result.

"adults_information" => array:1 [▼
 0 => array:6 [▼
  "first_name" => "Luke"
  "last_name" => "Greer"
  "dob_day" => "08"
  "dob_month" => "01"
  "dob_year" => 1935
  "gender" => "M"
]]
"contact_name" => "Eula Dennis"
"mobile_number" => "7308001726"

What I want is to create extra field after dob_year such as dob which constist of calculation of dob_day,"dob_year","dob_month" . I want some line of code such that when I do dd($request->all()) . I want to get the output like this .

"adults_information" => array:1 [▼
 0 => array:6 [▼
  "first_name" => "Luke"
  "last_name" => "Greer"
  "dob_day" => "08"
  "dob_month" => "01"
  "dob_year" => 1935
  "gender" => "M",
  "dob"=>"1935-01-08"
]]
"contact_name" => "Eula Dennis"
"mobile_number" => "7308001726"

I tried $request->add() but it didn't work . Any help will be appriciated

3
  • $request->adults_information[0][] = ''; Commented Mar 13, 2018 at 8:14
  • Because it is $request->request->add(); Commented Mar 13, 2018 at 8:30
  • I think that you need php array_push Commented Mar 13, 2018 at 9:53

4 Answers 4

3

The correct syntax is not $request->add but $request->request->add.

So:

$request->request->add([
    'adults_information'=>$request->adults_information + ['dob' => '1935-01-08']
]);
Sign up to request clarification or add additional context in comments.

Comments

3
    $inputs = $request->all();
    foreach($inputs['adults_information'] as $key => $info)
    {
        $dob = $info['dob_year'].'-'.
               $info['dob_month'].'-'.
               $info['dob_day'];

        $inputs['adults_information'][$key]['dob'] = $dob;
    }
    $request->merge($inputs);
    dd($request->all());

Comments

1

Hi u can use merge() with array_push to push a nested array.

$adults_information = $request->adults_information;
    $insert = [
        "first_name" => "Luke",
        "last_name" => "Greer",
        "dob_day" => "08",
        "dob_month" => "01",
        "dob_year" => 1935,
        "gender" => "M",
        "dob"=>"1935-01-08"
    ];
    array_push($adults_information, $insert);

    $request->merge('adults_information', $adults_information);

https://laravel.com/docs/5.6/requests

Hope this helps

Comments

0

You can use replace method for appending item to request object. For more details you can check Laravel API docs https://laravel.com/api/5.6/Illuminate/Http/Request.html#method_replace . For example.

$data = $request->all();
$data['appending_data_1'] = 'dummy value';
$data['appending_data_2'] = 'dummy value';
$request->replace($data);

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.