将表单中的隐藏输入发送到Laravel中的控制器

时间:2015-11-16 23:44:40

标签: php laravel laravel-5 laravel-5.1 blade

我有一个

创建表单

{!! Form::open(array('url' => '/cpe/store', 'class' => 'form-horizontal', 'role' =>'form')) !!}

              <h3 class="nomargin">Create CPE </h3>

              <div class="mb10">
                  <label class="control-label">Account ID </label>
                  <input type="text" class="form-control" name="account_id" value="{{$account->account_id}}">
              </div>


              <div class="mb10">
                  <label class="control-label">Max Up</label>
                  <input type="text" class="form-control" name="max_up" value="30000">
              </div>

              <div class="mb10">
                  <label class="control-label">Max Down</label>
                  <input type="text" class="form-control" name="max_down" value="50000" >
              </div>



              <a href="/account/" class="btn btn-danger ">Go back to account </a>

              {!! Form::hidden('$id', $account->account_id )!!}

              <button type="submit" class="btn btn-success ">Create</button>


            {!! Form::close();!!}

存储功能

public function store() {

        $inputs = Input::all();
        unset($inputs['_token']);


        $cpe_mac = $inputs['mac1'].$inputs['mac2'].$inputs['mac3'].$inputs['mac4'].$inputs['mac5'].$inputs['mac6'];

        $cpe = [];

        $cpe['cpe_mac'] = $cpe_mac;
        $cpe['bandwidth_max_up'] = (int)$inputs['max_up'];
        $cpe['bandwidth_max_down'] = (int)$inputs['max_down'];

        $json = json_encode($cpe);

        try {

            $url = 'http://172.16.139.129:1234/vse/vcpe';
            $ch = curl_init($url);

            if (FALSE === $ch)
                throw new Exception('failed to initialize');

            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 1);
            $result = curl_exec($ch);
            curl_close($ch);

            if (FALSE === $result)
                throw new Exception(curl_error($ch), curl_errno($ch));

            // ...process $result now
        } catch(Exception $e) {

            trigger_error(sprintf(
                'Curl failed with error #%d: %s',
                $e->getCode(), $e->getMessage()),
                E_USER_ERROR);

        }

        $result_json =  json_decode($result, true);


        dd($id); // Undefined variable: id


        if ( $result_json['status'] == '200') {
            return Redirect::to('/account/'.$id.'#hardware') ->with('success','The CPE was created succesfully!');
        } else {
            return Redirect::to('/account/'.$id.'#hardware') ->with('error', $result_json['message']);
        }

    }

我正在尝试将{!! Form::hidden('$id', $account->account_id )!!}传递给我的商店控制器,以便我可以在我的控制器中的store()函数内访问它。

但我一直得到Undefined variable: id

有人可以告诉我从表单向控制器发送隐藏输入的正确方法吗?

1 个答案:

答案 0 :(得分:2)

您的代码有两个问题:

  1. 您的隐藏输入应使用id,而不是$id
  2. 您的store方法应使用$inputs['id'],而不是直接访问$id