【问题标题】:Laravel get request post arrayLaravel 获取请求发布数组
【发布时间】:2015-11-17 15:05:20
【问题描述】:

我正在使用 Laravel 5 并试图从我发布的表单中获取值。 这适用于普通表单输入名称,例如:

$request->input('stripeToken')

但是,如果输入名称是 name="order['amount']" 之类的数组,则我无法获取该值。我试过了:

$request->input( "order['return_url']" )

有人对此有什么建议吗?

【问题讨论】:

    标签: laravel laravel-5


    【解决方案1】:

    使用点符号:

    $value = $request->input('order.return_url');
    

    【讨论】:

      【解决方案2】:

      在数组括号中省略引号

      <input name="order[amount]">
      <input name="order[amount2]">
      

      那么你可以得到例如值

      return $request->input('order')['amount'];
      return $request->input('order')['amount2'];
      

      return $request->get('order')['amount'];
      return $request->get('order')['amount2'];
      

      【讨论】:

      • 谢谢,您的回复对我帮助很大!
      【解决方案3】:

      Retrieving A Portion Of The Input Data 如果需要检索输入数据的子集,可以使用 only 和 except 方法。这两种方法都将接受单个数组或动态参数列表:

      $input = $request->only(['username', 'password']);
      
      $input = $request->only('username', 'password');
      
      $input = $request->except(['credit_card']);
      
      $input = $request->except('credit_card');
      

      【讨论】:

        【解决方案4】:

        这是一个简单的解决方案 使用数组概念及其索引

        $orders = $request->orders;
        

        并使用索引获取值

        $val = $orders[0];
        

        或者

        $val = $orders['return_url'];
        

        【讨论】:

          猜你喜欢
          • 2014-08-17
          • 2021-12-30
          • 1970-01-01
          • 1970-01-01
          • 2019-02-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-06-24
          相关资源
          最近更新 更多