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());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的