# Add data to request data \[solved\]

**URL:** https://discourse.cakephp.org/t/add-data-to-request-data-solved/9356
**Category:** Need Help
**Created:** [May 19, 2021, 5:45am UTC](https://discourse.cakephp.org/t/add-data-to-request-data-solved/9356 "2021-05-19T05:45:38Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)
#### Post date: [May 19, 2021, 5:45am UTC](https://discourse.cakephp.org/t/add-data-to-request-data-solved/9356/1 "2021-05-19T05:45:38Z")

</div>

4.x

I need to add a variable in a controller that does not come from a form.  
Before, in 3.x, I could add variables to the request-\>data array :  
$this-\>request-\>data[‘new\_var’] = $value;

I tried several ways with getData but was unsuccessful :  
$this-\>request-\>getData()-\>new\_var = $value;  
$this-\>request-\>getData(‘new\_var’) = $value;

It seems that getData is read only;

How can I do this in 4.x ?

---

<div class="post-metadata">

### Author: ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)
#### Post date: [May 19, 2021, 9:26am UTC](https://discourse.cakephp.org/t/add-data-to-request-data-solved/9356/2 "2021-05-19T09:26:50Z")

</div>

As far as I know “this-\>request-\>getData()” is immutable

---

<div class="post-metadata">

### Author: ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)
#### Post date: [May 19, 2021, 9:52am UTC](https://discourse.cakephp.org/t/add-data-to-request-data-solved/9356/3 "2021-05-19T09:52:56Z")

</div>

Thank you but that does not help.  
How can we add variables in the request data ?  
Of course, you can do :  
$article-\>my\_var = $value;  
and this works fine.

But let’s take an example :  
some times, I need to assign a particular name to an article.  
So i will write :  
$article-\>name = new\_name;  
It’s OK.

But then, validation will fail and I need validation for other cases.

Then, as I am writing this post, I wondered if validation could by bypassed.  
I’ll check the cookbook and let you know.

---

<div class="post-metadata">

### Author: ![Mentis](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mentis/32/994_2.png) [@Mentis](https://discourse.cakephp.org/u/Mentis)
#### Post date: [May 19, 2021, 1:59pm UTC](https://discourse.cakephp.org/t/add-data-to-request-data-solved/9356/4 "2021-05-19T13:59:37Z")

</div>

Can you do this?

```php
$data = ['name' => 'New Name'] + $this->getRequest()->getData();
$this->Articles->patchEntity($article, $data);

```

Any ‘name’ key in the request data will be overwritten by the value assigned.

---

<div class="post-metadata">

### Author: ![yousuo](https://avatars.discourse-cdn.com/v4/letter/y/898d66/32.png) [@yousuo](https://discourse.cakephp.org/u/yousuo)
#### Post date: [May 19, 2021, 2:37pm UTC](https://discourse.cakephp.org/t/add-data-to-request-data-solved/9356/5 "2021-05-19T14:37:26Z")

</div>

I usually convert it to array and add them in (using it on Cake 4.2)

```
$receivedData = $this->request->getData();
$receivedData['type'] = 'my custom data';
$this->Articles->patchEntity($article, $receivedData );
```

---

<div class="post-metadata">

### Author: ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)
#### Post date: [May 19, 2021, 3:06pm UTC](https://discourse.cakephp.org/t/add-data-to-request-data-solved/9356/6 "2021-05-19T15:06:04Z")

</div>

You can add data to the request:

```php

$this->request = $this->request->withData('key_name', $new_value);

```

Here is a useful section of the book: [Using immutable Responses](https://book.cakephp.org/4/en/controllers/request-response.html#common-mistakes-with-immutable-responses)

---

<div class="post-metadata">

### Author: ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)
#### Post date: [May 19, 2021, 4:40pm UTC](https://discourse.cakephp.org/t/add-data-to-request-data-solved/9356/7 "2021-05-19T16:40:11Z")

</div>

Thank you.  
This seems to be the right way.  
I’m not at work currently but I will try this tomorrow.

---

<div class="post-metadata">

### Author: ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)
#### Post date: [May 19, 2021, 10:36pm UTC](https://discourse.cakephp.org/t/add-data-to-request-data-solved/9356/8 "2021-05-19T22:36:56Z")

</div>

Which solution is best will very much depend on whether you want this particular bit of data to be set all the time, despite what might be posted, or if you want to use it only as a default when something else isn’t given.

---

<div class="post-metadata">

### Author: ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)
#### Post date: [May 21, 2021, 9:37am UTC](https://discourse.cakephp.org/t/add-data-to-request-data-solved/9356/9 "2021-05-21T09:37:30Z")

</div>

[dreamingmind](https://discourse.cakephp.org/u/dreamingmind)  
Thank you for this solution. It works perfectly.

There are many cases when you have to add data into the request.  
Examples :  
When you upload a file, the name is not in the request.  
If you need an information from another (non related) table or even from another DB.

In any case, this helps a lot.
