douyou7072 2018-12-15 11:21
浏览 121
已采纳

laravel请求索引不能列出数组分类

Note: my question is not duplicate

I have form in out of site and use web api laravel 5.7, now is send these parameters as post:

estate: "545345"
id_number: "43534545"
serial_number: "435435345"
type[code_id]: "45345345435"
type[company]: ""
type[country]: ""
type[maintenance_period]: ""
type[model]: "5435345435"
type[name]: "5345345345"
waranty[company_id]: "0"
waranty[duration]: ""
waranty[end]: ""
waranty[start]: ""

part of form:

<input type="text" name="estate" required="">
<input type="text" name="id_number" required="">
<input type="text" name="type[code_id]">
<input type="text" name="type[company]">

Now I want to get type and waranty array: same as for example:

$type = ['name' => 'test','company' => '...', 'model' => '...' ]

I try these codes:

$type = $request->input('type.*'); // NULL
and
$type = $request->input('type'); // NULL
and 
$type = Input::get('type'); // NULL
and
$in = $request->all();
$type = $in['type']; // NULL
AND
$request->get('type'); // NULL
AND
$request->post('type') // NULL

Doesn't work :( , only I can get single variable, by this method: (but I need array)

$in = $request->all();
$in['type[code_id]'] ;

And I test type[][code_id],type[][name],... in form but not work :(

My axios method:

serial = function (id) {
    var srl = $(id).serializeArray();
    var post = {};
    for (const k in srl) {
        let item = srl[k];
        post[item.name] = item.value;
    }
    return post;
}

axios.post(url, serial("#frm")).then(function (res) {// succs axios ;

     console.log(res);
}).catch(err => {// error axios ;
    console.log(err);
}); // end axios ;

What is the Laravel native solution ? for this problem ?

note: I can create an function for solve this problem but I need laravel native solution.

  • 写回答

1条回答 默认 最新

  • dongxiangqian1855 2019-07-10 06:38
    关注

    You can handles arrays with this function:

    function categorizedArray($array){
        $ret = [] ;
        $re = '/(.*)\[(.*)\]/U';
        foreach ($array as $i => $value) {
            preg_match_all($re, $i, $matches, PREG_SET_ORDER, 0);
            if ($matches == []){
                $ret[$i] = $value ;
            }else{
                $ret[$matches[0][1]][$matches[0][2]] = $value ;
            }
        }
        return $ret;
    }
    

    for exmp: ( We assume the function define as helper )

    $all = categorizedArray($request->all());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容