Laravel request()-> all()不能获取所有输入

时间:2018-12-08 17:48:02

标签: javascript php laravel

通过POST格式发送数据。控制器为什么看不到所有值?我一直在进行验证,因此即使将其设置为'required'也不会改变任何内容...

表单摘要:

 <form action="{{ Protocol::home() }}/offers/make" method="POST" id="sendOffer">
        <div class="modal-body">
            <meta name="csrf-token" content="{{ csrf_token() }}">
            <!-- Price -->
            <div class="form-group" style="display:none;">
                <label>{{ Lang::get('ads/show.lang_your_price') }}</label>
                <input type="text"  id="price_id" name="price" value="0">
                <span class="help-block">{{ Lang::get('ads/show.lang_the_amount_required') }} <b>{{ Helper::getPriceFormat($ad->price, $ad->currency) }}</b></span>
            </div>
            <!-- location -->
            <div class="form-group">
                <label>location label</label>
                <input type="text" placeholder="Andover MA" id="location_id" class="form-control" name="location_name">
            </div>
            <!-- Email Address -->
            <div class="form-group">
                <label>email label</label>
                <input type="email" required="" placeholder="email" id="email_name" class="form-control" name="email_name">
            </div>
            <!-- Phone -->
            <div class="form-group">
                <label>phone label</label>
                <input type="text" maxlength="12" placeholder="555-867-5309" id="friendNumber" class="form-control" name="phone_name">
            </div>
            <!--Time section-->
            <div class="form-group">
                <label>The time</label>
                <input type="time" id="time_id" name="time_name"
                       min="9:00" max="18:00" required>
            </div>
            <!-- Post ID -->
            <div class="form-group">
                <label>{{ Lang::get('ads/show.lang_post_id') }} (for reference)</label>
                <input type="text" readonly="" placeholder="{{ Lang::get('ads/show.lang_post_id') }}" id="postID" value="{{ $ad->ad_id }}" class="form-control" name="ad_id">
            </div>
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-success">{{ Lang::get('ads/show.lang_send_offer') }}</button>
            </div>
        </form>

控制器:

/**
 *  Make New Offer
 */
public function make(Request $request)
{
    // Check ajax request
    if ($request->ajax()) {

            $rules = array(
            'location_name' => '',
            'email_name' => '',
            'phone_name' => '',
            'time_name' => '',
            'ad_id' => 'required'
        );
        //run rules
        $validator = Validator::make($request->all(), $rules);

        if ($validator->fails()) {
            // error
            $response = array(
                'status' => 'error',
                'msg'    => __(print_r($request->all())),
            );
            return Response::json($response);
        }else{
            // Get Inputs
            $price = $request->get('price');
            $location_name  = $request->input('location_name');
            $email_name = $request->input('email_name');
            $phone_name = $request->input('phone_name');
            $time_name = $request->input('time_name');
            $ad_id = $request->input('ad_id');
            $input = $request->all();
            //let's figure it out:
            dd($input);
        // Success test
          $response = array(
          'status' => 'success',
          'msg'    => __('return/success.lang_offer_submitted'),
           );
return Response::json($response);

控制台中的输出(出于某些原因仅显示价格和ad_id)

 array:2 [
  "price" => "0"
  "ad_id" => "1988726232"
]

路线:

// Make an Offer
Route::post('offers/make', 'Ads\OffersController@make');

1 个答案:

答案 0 :(得分:0)

该错误是由于我忘记在ajax请求 smh

中包含这些字段而导致的
    var _root = $('#root').attr('data-root');
    var offerPrice = document.getElementById('offerPrice').value;
    var postID = document.getElementById('postID').value;
    var location_name = document.getElementById('location_name').value;
    var email_name = document.getElementById('email_name').value;
    var phone_name = document.getElementById('phone_name').value;
    var time_name = document.getElementById('time_name').value;
    $.ajax({
        type: "POST",
        url: _root + '/offers/make',
        data: {
            price: offerPrice,
            ad_id: postID,
            location_name: location_name,
            email_name: email_name,
            phone_name: phone_name,
            time_name: time_name
        },