Home > Backend Development > PHP Tutorial > laravel5.2.9 - php laravel adds $request->all() field

laravel5.2.9 - php laravel adds $request->all() field

WBOY
Release: 2016-09-14 09:41:26
Original
1689 people have browsed it

php code

<code>public function store(Request $request)
{
    
    $name = $request->get('namne', 0);
    
    $newname = $request->set('newname', 0);  
    
    // 前端提交 `data` 里面没有 `newname` 这个字段,
    // 需要在这里处理一些业务逻辑,新增一个字段然后到下面一起保存起来
    
    // $request->newname = 'newname' 
    // 尝试过这样好像没写进入
    
    //想在保存数据前,添加一个字段 ;
    // $request->all()->'新的字段名称' = '我是新加的字段值';
    // 这个时候 $request->all() 会多出来上面新添加的字段
    
    $post = Post::create($request->all());

}</code>
Copy after login
Copy after login

vue code

<code>this.$http.post('/backend/post', data).then(function(result) {

})</code>
Copy after login
Copy after login

Master, does anyone know how to add a field in $request->all() before saving?

Reply content:

php code

<code>public function store(Request $request)
{
    
    $name = $request->get('namne', 0);
    
    $newname = $request->set('newname', 0);  
    
    // 前端提交 `data` 里面没有 `newname` 这个字段,
    // 需要在这里处理一些业务逻辑,新增一个字段然后到下面一起保存起来
    
    // $request->newname = 'newname' 
    // 尝试过这样好像没写进入
    
    //想在保存数据前,添加一个字段 ;
    // $request->all()->'新的字段名称' = '我是新加的字段值';
    // 这个时候 $request->all() 会多出来上面新添加的字段
    
    $post = Post::create($request->all());

}</code>
Copy after login
Copy after login
vue code

<code>this.$http.post('/backend/post', data).then(function(result) {

})</code>
Copy after login
Copy after login
Master, does anyone know how to add a field in

$request->all() before saving?

<code>$request->all() // 这是一个数组吧,array_merge() 是你的需求么
</code>
Copy after login
<code>$array = $request->all();
$array['test'] = 'test';

$post = Post::create($array);</code>
Copy after login

Several commonly used methods to operate input:

<code class="php">// 合并输入,如果有相同的key,用户输入的值会被替换掉,否则追加到 input
$request->merge(['foo' => 'bar', ....]);

// 替换所有输入
$request->replace([..])

// 设置某参数,如果存在则替换,否则新增
$request['foo'] = 'bar';

// 移除某参数
unset($request['foo'])</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template