Laravel按月列出选择选项

时间:2018-08-28 12:37:54

标签: javascript ajax laravel laravel-controller

我对此有疑问。 IT说:“函数App \ Http \ Controllers \ ViewsController :: OBviews()的参数太少,传递了0个,恰好期望有1个”

我的控制器:

 public function OBviews($date)
    {
        $masterIds = DB::select('SELECT * FROM o_b_masters OBM INNER JOIN users u on OBM.emp_id = u.id INNER JOIN o_b_requests OBR 
        on OBR.details_id = OBM.obmaster_id WHERE u.id =? AND MONTH(OBM.date_filed) = ?', [Auth::id(), $date]);

         return response()->json($masterIds); 
    }

我的路线:

Route::GET('/obView/{date}', 'ViewsController@OBviews')->name('OBRequest');

我的js:

 $('select').on('change', function (e) {
var optionSelected = $("option:selected", this);
var valueSelected = this.value;
  alert(valueSelected);
    $.ajax({
            url: 'obView',
            type: 'GET',
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
            data: 'date='+valueSelected,
            success: function (data) {
                if (data['success']) {
                    alert(data['success']);
                } else if (data['error']) {
                    alert(data['error']);
                } else {
                    alert('Whoops Something went wrong!!');
                }
            },
            error: function (data) {
                alert(data.responseText);
            }
        });
  });

我的details.blade.php:

  <select name="month" class="form-control">
    <option value="01">January</option>
    <option value="02">February</option>
    <option value="03">March</option>
    <option value="04">April</option>
    <option value="05">May</option>
    <option value="06">June</option>
    <option value="07">July</option>
    <option value="08">August</option>
    <option value="09">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
  </select>

似乎那里有变化。没有任何东西传递给控制器​​。

1 个答案:

答案 0 :(得分:0)

您的网址需要一个日期段,但是在您的Ajax调用中,您没有指定日期段。

为此调整代码

 $('select').on('change', function (e) {
var optionSelected = $("option:selected", this);
var valueSelected = this.value;
  alert(valueSelected);
    $.ajax({
            url: 'obView/' + valueSelected,
            type: 'GET',
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
            success: function (data) {
                if (data['success']) {
                    alert(data['success']);
                } else if (data['error']) {
                    alert(data['error']);
                } else {
                    alert('Whoops Something went wrong!!');
                }
            },
            error: function (data) {
                alert(data.responseText);
            }
        });
  });